如何倾听PreferenceFragment中的偏好更改?

如上所述,我将PreferenceFragment子类化并在Activity中显示。 该文档解释了如何在此处侦听首选项更改,但仅限于PreferenceActivity的子类。 由于我没有这样做,我如何倾听偏好改变?

我试过在我的PreferenceFragment实现OnSharedPreferenceChangeListener,但它似乎并没有工作( onSharedPreferenceChanged似乎永远不会被调用)。

这是我的代码到目前为止:

SettingsActivity.java

 public class SettingsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Display the fragment as the main content. getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit(); } } 

SettingsFragment.java

 public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener { public static final String KEY_PREF_EXERCISES = "pref_number_of_exercises"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { //IT NEVER GETS IN HERE! if (key.equals(KEY_PREF_EXERCISES)) { // Set summary to be the user-description for the selected value Preference exercisesPref = findPreference(key); exercisesPref.setSummary(sharedPreferences.getString(key, "")); } } } 

的preferences.xml

 <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <EditTextPreference android:defaultValue="15" android:enabled="true" android:key="pref_number_of_exercises" android:numeric="integer" android:title="Number of exercises" /> </PreferenceScreen> 

此外,PreferenceFragment是否适合倾听偏好变化,还是应该在活动内部进行?

Solutions Collecting From Web of "如何倾听PreferenceFragment中的偏好更改?"

我相信你只需要在你的PreferenceFragment注册/注销Listener ,它就可以工作。

 @Override public void onResume() { super.onResume(); getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); } @Override public void onPause() { getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); super.onPause(); } 

根据你想要做什么,你可能不需要使用一个监听器。 对首选项的更改将自动提交给SharedPreferences

先前的解决scheme效果很好,在这里你可以看到Android v11以上的完整偏好活动:

 import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.ListPreference; import android.preference.PreferenceFragment; public class UserPreferencesV11 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Display the fragment as the main content. getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit(); } public static class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences); // set texts correctly onSharedPreferenceChanged(null, ""); } @Override public void onResume() { super.onResume(); // Set up a listener whenever a key changes getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); } @Override public void onPause() { super.onPause(); // Set up a listener whenever a key changes getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // just update all ListPreference lp = (ListPreference) findPreference(PREF_YOUR_KEY); lp.setSummary("dummy"); // required or will not update lp.setSummary(getString(R.string.pref_yourKey) + ": %s"); } } } 

所有其他的答案是正确的。 但是我更喜欢这个替代scheme,因为你立即有了导致改变的Preference实例。

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Preference pref = findPreference(getString(R.string.key_of_pref)); pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // do whatever you want with new value // true to update the state of the Preference with the new value // in case you want to disallow the change return false return true; } }); } 

这对我来说PreferenceFragment.onCreate()

 OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { showDialog(); } };