我有我的MainActivity,它调用我的设置活动:
Intent i = new Intent(this, SettingsActivity.class);
startActivityForResult(i, RESULT_SETTINGS);
“device_name”是一个没有默认值或值的 EditTextPreference。
<EditTextPreference
android:key="device_name"
android:title="Device Name"
android:selectAllOnFocus="true"
android:inputType="textCapWords"
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />
我的SettingsActivity扩展了 PreferenceActivity。
onPostCreate我打电话bindPreferenceSummaryToValue(findPreference("device_name"));
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
侦听器的主要焦点是设置首选项摘要,对于 editTextPreference 我要检查值是 null 还是空并将其替换为Build.MODEL
.
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
}
else if (preference instanceof EditTextPreference)
{
EditTextPreference editTextPreference = (EditTextPreference) preference;
//editTextPreference.getText();
if (stringValue.trim().equals("") || stringValue.equals(null))
{
Log.i("valuesUpdate", "Yes");
editTextPreference.setText(Build.MODEL + "_1");
Log.i("valuesUpdate", editTextPreference.getTitle() + " = " + editTextPreference.getText());
editTextPreference.setSummary(Build.MODEL + "_2");
}
else
{
Log.i("valuesUpdate", "No?");
editTextPreference.setSummary(stringValue);
}
}
else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
当我从MainActivity获取设置时,我使用以下代码:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.getAppContext());
String android_id = sharedPreferences.getString("device_name", "UNKNOWN_DEVICE");
当我第一次启动应用程序时,android_id按预期返回“UNKNOWN_DEVICE”。
打开/关闭SettingsActivity后,android_id按预期返回“XT1060_1”。
如果我单击以编辑设置"device_name",它也会按预期在文本字段中填充 "XT1060_1" 。
问题:当我将设置“device_name”编辑为空字符串,然后在我的设备上单击“确定”时,“设备名称”的摘要按预期显示“XT1060_2”,日志显示:
valuesUpdate﹕ Yes
valuesUpdate﹕ Device Name = XT1060_1
(所以getText()在setText()之后直接在SettingsActivity中工作得很好)
但是当我重新打开"device_name"的设置时,文本字段仍然是空的。
同样,一旦我关闭SettingsActivity并尝试从MainActivity获取设置,android_id也会返回一个空字符串。
一旦我再次重新打开SettingsActivity,它就可以工作(就像上面的第 2 点一样)。