我正在尝试使MultiSelectListPreference起作用,但是它不能保存,请记住用户的选择。 onSharedPreferenceChanged仅适用于EditText和ListPreference,MultiSelectListPreference不会调用它,因此...
我尝试实现Preference.OnPreferenceChangeListener并重写onPreferenceChange,并且尝试以不同的方式调用它,就像您在代码中将看到的那样,但未调用它们。
package com.adc.settingsapp;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.MultiSelectListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceChangeListener {
SharedPreferences sharedPreferences;
public SettingsFragment() {
// Required empty public constructor
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.pref_configuration);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
onSharedPreferenceChanged(sharedPreferences, "pref_display_name");
onSharedPreferenceChanged(sharedPreferences, "pref_language");
onSharedPreferenceChanged(sharedPreferences, "pref_multilist");
setDefaultValuesForMultiSelectListPreference();
MultiSelectListPreference mMultiSelectListPreference = (MultiSelectListPreference) findPreference("pref_multilist");
mMultiSelectListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast.makeText(getActivity(), "onPreferenceCHangeLIstner", Toast.LENGTH_SHORT);
return true;
}
});
}
// set the default values
private void setDefaultValuesForMultiSelectListPreference() {
MultiSelectListPreference mMultiSelectListPreference = (MultiSelectListPreference) findPreference("pref_multilist");
Set<String> value = new HashSet<String>(Arrays.asList("Value1", "Value3"));
mMultiSelectListPreference.setValues(value);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
// evocato quando cambiano i settaggi,
// evocato anche quando si carica l'activity per la prima volta,
// probabilmente lo chiama onSharedPreferenceChanged in onCreatePreference
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference preference = findPreference(key);
//Toast.makeText(getActivity(), "onSharedPreferenceChanged", Toast.LENGTH_SHORT).show();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(sharedPreferences.getString(key, ""));
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else if( preference instanceof EditTextPreference) {
preference.setSummary(sharedPreferences.getString(key, "NoName"));
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast.makeText(getActivity(), "onPreferenceChange", Toast.LENGTH_SHORT).show();
return true;
}
}
一旦我了解如何管理用户选择,便会保存它(基于SharedPreference)。