Make your PreferenceActivity implement
SharedPreferences.OnSharedPreferenceChangeListener
declare in PreferenceActivity :
SharedPreferences prefs;
initialize in onCreate :
SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this); prefs = sPrefs;
and register with the general preference change listener
prefs.registerOnSharedPreferenceChangeListener(this);
do what Steve said in the onResume and onPause .
implementation of the onSharedPreferenceChanged listener:
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Log.d("SettingsActivity","onSharedPreferenceChanged LISTENER FIRED"); if (key.equals(getString(R.string.key_call))) { //if call true if (sharedPreferences.getBoolean(getString(R.string.key_call), false)) { Preference preference = findPreference(getString(R.string.key_record)); preference.setEnabled(false); } else { // if call false Preference preference = findPreference(getString(R.string.key_record)); preference.setEnabled(true); } } if (key.equals(getString(R.string.key_record))) { //if record true if (sharedPreferences.getBoolean(getString(R.string.key_record), false)) { Preference preference = findPreference(getString(R.string.key_call)); preference.setEnabled(false); } else { // if record false Preference preference = findPreference(getString(R.string.key_call)); preference.setEnabled(true); } } }
In this case, I have 2 mutually exclusive settings in PreferenceActivity . Call and record. If both boxes are unchecked, both can be checked, but when the user checks one of them, the other becomes grayed out. When the user cancels the selected preferences, the user can check another.
In both cases, other settings may be dependent, which can be worked out with the android:dependancy in the XML file.
PrimoΕΎ Majhen Jan 25 '15 at 15:57 2015-01-25 15:57
source share