带有false persistent的EditTextPreference不会调用onSharedPreferenceChanged

时间:2015-05-22 10:20:55

标签: android sharedpreferences android-preferences

我有这个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"

1 个答案:

答案 0 :(得分:3)

您必须在PreferenceActivityPreferenceFragment的代码中附加自定义侦听器:

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