我有这个EditTextPreference
<EditTextPreference
android:title="@string/settings_server"
android:summary="@string/server_name_message"
android:persistent="false"
android:key="SERVER_NAME" />
但是对于persistent =“false”,它不会调用onSharedPreferenceChanged
方法。
如何检测此事件并android:persistent="false"
答案 0 :(得分:3)
您必须在PreferenceActivity
或PreferenceFragment
的代码中附加自定义侦听器:
EditTextPreference prefServerName = (EditTextPreference) findPreference("SERVER_NAME");
prefServerName.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String value = (String) newValue;
// do what you need
return true; // indicates you processed the new value
}
});