How to do opposite of of preference attribute android:dependency?

后端 未结 5 1883
抹茶落季
抹茶落季 2020-12-07 18:36

Is there XML attribute that does the exact opposite of android:dependency?

What I would like the dependent preference to be enabled when the other is N

5条回答
  •  清歌不尽
    2020-12-07 19:10

    Dmytro Zarezenko asked what if you wanted some dependencies to be enabled when the preference on which they depend is true and some to be enabled when that preference is false.

    Use the method described above to set the all the dependant preferences of one type (which ever have the greater number). Then (with the class having implements OnSharedPreferenceChangeListener) have code like this in the Preference Activity and/or Preference Fragment:

    @Override
    public void onResume()
    {
        super.onResume();
        sharedPreferences.registerOnSharedPreferenceChangeListener(this);
    }
    @Override
    public void onPause()
    {
        super.onPause();
        sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
    }
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
        if (key.equals("pref_that_they_depend-upon")
        {
            // Iterate over the preferences that need to be enabled or disabled,
            // lets say there is just one called the_awkward_one.
            Preference preference = findPreference("the_awkward_one");
            // Or preference.setEnabled(! sharedPreferences.getBoolean(("pref_that_they_depend-upon", defaultValue));
            preference.setEnabled(sharedPreferences.getBoolean(("pref_that_they_depend-upon", defaultValue));
        }
    }
    

提交回复
热议问题