0

アプリで textView2 を共有設定に保存したい。これは、チェックボックスの状態を保存する現在の共有設定コードです (チェックされている/チェックされていない)。

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 を保存するために新しい共有設定を作成する方法がよくわかりません。

4

2 に答える 2

3

できません。

SharedPreferences クラスは、プリミティブ データ型の永続的なキーと値のペアを保存および取得できる一般的なフレームワークを提供します。SharedPreferences を使用して、boolean、float、int、long、string などのプリミティブ データを保存できます。

共有設定を使用してドキュメントから

TextViewの値を保存できると思っていましたが

//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();
    }

これらのメソッドの使用方法

このコードを、TextView の値を保存する場所に配置します。

//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"));
于 2013-06-13T11:47:44.440 に答える
1

共有設定に文字列を簡単に保存できます:
共有設定の使用方法がわからない場合は、これが非常に役立ちます。

TextViewsに関しては、保存はどうtextView2.getText()ですか?
TextView.getText()明らかに、TextView が表示しているテキストを返します。他の TextView プロパティが必要な場合は、こちらを参照してください。

于 2013-06-13T12:08:57.553 に答える