2013-06-13 4 views
0

내 응용 프로그램이 공유 환경 설정에서 textView2를 저장하길 원합니다. 이것은 체크 박스의 상태를 저장하는 현재 공유 환경 설정 코드입니다 (선택/선택 해제).공유 환경 설정에서 textView를 어떻게 저장합니까?

private boolean getFromSP(String key){ 
SharedPreferences bifrostPrefs = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE); 
return bifrostPrefs.getBoolean(key, false); 
} 
private void saveInSp(String key,boolean value){ 
SharedPreferences bifrostPrefs = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = bifrostPrefs.edit(); 
editor.putBoolean(key, value); 
editor.commit(); 
} 

내가 정말 그렇게 난 정말 내 textview2을 절약 할 수있는 새로운 공유 prefrence을 만들 것입니다 방법을 모르는 전에 공유 prefrences와 함께 일하지 않는다.

+2

공유 환경 설정에보기를 저장할 수 없습니다. 하지만 문자열을 저장할 수 있으므로 putBolean 대신 putString을 사용하면 다른 모든 것은 동일하게 작동합니다. –

답변

3

수 없습니다.

된 SharedPreferences 클래스를 저장하고 기본 데이터 유형의 지속적인 키 - 값 쌍을 검색 할 수있는 일반적인 프레임 워크를 제공합니다. SharedPreferences를 사용하여 기본 데이터 ( 부울, 부동 소수점, 정수, 정수 및 문자열)를 저장할 수 있습니다. 문서에서

Using Shared Preferences

.. 당신은 텍스트 뷰

//To get stored value 
    private String getString(String key){ 
     SharedPreferences bifrostPrefs = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE); 
     return bifrostPrefs.getString(key, ""); 
    } 

의 값을 저장할 수있는 알이-생각

//To Save value 
    private void saveString(String key, String value){ 
     SharedPreferences bifrostPrefs = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = bifrostPrefs.edit(); 
     editor.putString(key, value); 
     editor.commit(); 
    } 

이러한 방법을 사용하는 방법

당신이 공유 환경 설정을 사용하는 방법을 모르는 경우
, TextViews에 관해서는 this will be of great value to you.

, 방법 :

는 쉽게 공유 환경 설정에서 문자열을 저장할 수 있습니다

//To save value of TextView 
if (!TextUtils.isEmpty(aTextView.getText())) { 
    saveString("aTextView", aTextView.getText().toString()); 
} 

//To Read and show into TextVIew 
aTextView.setText(getString("aTextView")); 
+0

하지만 어디서 어떻게 2 가지 방법을 호출할까요? – Guy

+0

@ user2454356 내 편집보기 –

+0

Thank you! 매력처럼 작동합니다. 나는 자바에 익숙하지 않아서 아직도 많이 혼란 스럽지만 더 많은 앱을 만드는 것이 더 쉬워지기를 바랍니다. – Guy

1

텍스트 뷰의 값을 저장하려면이 코드를 넣어 절약에 대해 textView2.getText()?
TextView.getText()은 TextView가 표시하는 텍스트를 분명히 반환합니다. 다른 TextView 속성이 필요한 경우 consult this.

관련 문제