如何侦听 PreferenceFragment 中的偏好更改? [英] How to listen for preference changes within a PreferenceFragment?
问题描述
如此处所述,我正在继承 PreferenceFragment 并显示它在一个活动中.该文档解释了如何在此处侦听偏好更改,但仅限如果您子类化 PreferenceActivity.既然我不这样做,我该如何监听偏好变化?
As described here, I am subclassing PreferenceFragment and displaying it inside an Activity. That document explains how to listen for preference changes here, but only if you subclass PreferenceActivity. Since I'm not doing that, how do I listen for preference changes?
我尝试在我的 PreferenceFragment 中实现 OnSharedPreferenceChangeListener 但它似乎不起作用(onSharedPreferenceChanged
似乎从未被调用).
I've tried implementing OnSharedPreferenceChangeListener in my PreferenceFragment but it does not seem to work (onSharedPreferenceChanged
never seems to get called).
这是我目前的代码:
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 是否是监听偏好更改的正确位置,还是我应该在 Activity 中执行此操作?
Also, is the PreferenceFragment even the right place to listen for preference changes or should I do it within the Activity?
推荐答案
我相信您只需要在 PreferenceFragment
中注册/取消注册 Listener
就可以了.
I believe you just need to register/unregister the Listener
in your PreferenceFragment
and it will work.
@Override
public void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
根据您想要做什么,您可能不需要使用侦听器.对首选项的更改会自动提交到 SharedPreferences
.
Depending on what you want to do you may not need to use a listener. Changes to the preferences are committed to SharedPreferences
automatically.
这篇关于如何侦听 PreferenceFragment 中的偏好更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!