Android 首选项何时保存到文件?
我有一个EditTextPreference,我不知道该字符串何时保存到共享首选项文件中Android 首选项何时保存到文件?,android,preference,edittextpreference,Android,Preference,Edittextpreference,我有一个EditTextPreference,我不知道该字符串何时保存到共享首选项文件中 如果我以空字符串开始,并将其更改为“Hello”,那么此更新何时保存?我必须手动保存吗?提交时,通过调用Editor.commit()或Editor.apply() 提交时,通过调用Editor.commit()或Editor.apply()来查看 请参见查看源代码的,该字符串将保留在setText()方法中 因此,文本更改后,它将被提交到SharedReferences文件。查看源代码,该字符串将保留在s
如果我以空字符串开始,并将其更改为“Hello”,那么此更新何时保存?我必须手动保存吗?提交时,通过调用
Editor.commit()
或Editor.apply()
提交时,通过调用
Editor.commit()
或Editor.apply()
来查看
请参见查看源代码的,该字符串将保留在setText()方法中
因此,文本更改后,它将被提交到SharedReferences文件。查看源代码,该字符串将保留在setText()方法中
因此,文本更改后,它将提交到SharedReferences文件。编辑首选项时,可以使用以下代码:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString("Preference_Label", variable_name);
edit.commit(); // this commits the edit
编辑首选项时,可以使用以下代码:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString("Preference_Label", variable_name);
edit.commit(); // this commits the edit
我有一个EditTextPreference,不知道它何时提交该字符串
到共享首选项文件
如果查看文档,调用setText(String)
方法时会保存文本。此方法提交并将文本保存到SharedReferences。在调用该方法之前,将永远不会更新首选项。例如
EditTextPreference mPrefs = ...
//perform any manipulations on the string, not saved until you call setText()
String mText = "2";
mText += " + 2";
mText += " = 4";
// saves "2 + 2 = 4" to SharedPreferences
mPrefs.setText(mText);
我有一个EditTextPreference,不知道它何时提交该字符串
到共享首选项文件
如果查看文档,调用setText(String)
方法时会保存文本。此方法提交并将文本保存到SharedReferences。在调用该方法之前,将永远不会更新首选项。例如
EditTextPreference mPrefs = ...
//perform any manipulations on the string, not saved until you call setText()
String mText = "2";
mText += " + 2";
mText += " = 4";
// saves "2 + 2 = 4" to SharedPreferences
mPrefs.setText(mText);
这显然是错误的。这适用于使用
SharedReferences.Editor
,但与EditTextPreference
无关。乔尔的回答是正确的;当你调用setText()
时,它会保存到SharedReferences
。你说得对-对不起,我不知道我怎么会错过问题的这一部分-我想我回答得有点快-给Joel一个正确的答案;-)但是,“在幕后”,调用setText(text)
;-)时会调用Editor.commit()
)那是真的!在幕后,人们正在打电话。然而,这有点超出了他的范围question@Joel是的,我知道,这就是为什么正确答案应该归功于ofc;-)这显然是错误的。这适用于使用SharedReferences.Editor
,但与EditTextPreference
无关。乔尔的回答是正确的;当你调用setText()
时,它会保存到SharedReferences
。你说得对-对不起,我不知道我怎么会错过问题的这一部分-我想我回答得有点快-给Joel一个正确的答案;-)但是,“在幕后”,调用setText(text)
;-)时会调用Editor.commit()
)那是真的!在幕后,人们正在打电话。然而,这有点超出了他的范围question@Joel是的,我知道,这就是为什么正确答案应该归功于ofc;-)正如Geobits指出的那样,给Joel而不是我正确的答案;-)正如Geobits指出的那样,给Joel而不是我正确的答案;-)