我有一个问题 onSharedPreferenceChanged 仅在 MultiSelectListPreference 发生更改时才第一次调用。我打开我的设置活动并更改正常工作的值并且 onSharedPreferenceChanged 被调用。如果我再次打开对话框,它会显示选择的正确条目。我选择另一个条目并点击确定。onSharedPreferenceChanged 现在应该被调用但不是。如果我现在再次打开对话框,则不会选择任何条目。我错过了什么还是我做错了什么?
这是我的preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<MultiSelectListPreference
android:key="operations"
android:title="@string/pref_operations"
android:dialogTitle="@string/pref_operations"
android:entries="@array/pref_operations_entries"
android:entryValues="@array/pref_operations_values"
android:defaultValue="@array/pref_operations_default" />
</PreferenceScreen>
我的设置片段
public class SettingsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener
{
public static final String KEY_OPERATIONS_PREFERENCE = "operations";
private MultiSelectListPreference operationsPreference;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
operationsPreference = (MultiSelectListPreference) getPreferenceScreen().findPreference(KEY_OPERATIONS_PREFERENCE);
}
@Override
public void onResume()
{
super.onResume();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
Set<String> operations = operationsPreference.getValues();
String summary = "";
for (String s : operations)
summary += s + " ";
operationsPreference.setSummary(summary);
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause()
{
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
if (key.equals(KEY_OPERATIONS_PREFERENCE))
{
Set<String> operations = operationsPreference.getValues();
String summary = "";
for (String s : operations)
summary += s + " ";
operationsPreference.setSummary(summary);
}
}
}