OnPreferenceChangeListener在调用setValue(...)时触发

时间:2014-05-27 21:05:50

标签: android android-preferences listpreference

使用onPreferenceChange方法时,是否有任何理由不调用 ListPreference 方法的setValue(...)

onPreferenceChange被调用的唯一时间是我实际触摸 ListPreference 并进行选择。但是当我以编程方式更改 ListPreference 的值时,它不会被调用。

这是我的setOnPreferenceChangeListener方法定义:

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
    ...
    // Load the preferences.xml
    addPreferencesFromResource(R.xml.preferences);

    // Set the summary for each Preference by reading its defaultValue
    PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.preferences, false);

    // Define a change listener
    final String ORIENTATION_KEY = mResources.getString(R.string.pref_quick_orientation_key);
    ListPreference preference = (ListPreference) findPreference(ORIENTATION_KEY);
    preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            // Do something here...
            return true;
        }
    });
    ...
    // Somewhere else in my code, using this code to change the value of the Preference
    final String ORIENTATION_KEY = mResources.getString(R.string.pref_quick_orientation_key);
    ListPreference preference = (ListPreference) findPreference(ORIENTATION_KEY);
    preference.setValue(mResources.getString(R.string.orientation_portrait));
    ...
}

0 个答案:

没有答案