【发布时间】:2014-11-24 17:45:22
【问题描述】:
当我尝试在用户选择首选项时设置摘要时,它通常会保存。但是当我的应用重新启动时,摘要就消失了。
这是我为ListPreference 和EditTextPreference 设置摘要的代码:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){
Preference pref = findPreference(key);
// I feel the problem is happened here
if (pref instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
pref.setSummary(listPref.getEntry());
}
// Same problem here
if (pref instanceof EditTextPreference) {
EditTextPreference editText = (EditTextPreference) pref;
pref.setSummary(editText.getEntry().toString());
}
}
有什么问题吗?
【问题讨论】:
-
你应该在 oncreate 中设置摘要
-
参数呢?此参数 [SharedPreferences sharedPreferences, String key] 与 Preference pref = findPreference(key) 相关联。在onCreate中,这个参数(String key)不可用。
-
如果不是,则在xml中添加android:key
-
但我已经为 xml 中的每个首选项添加了 android:key。