java 如何做与偏好属性android:dependency相反的事情?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3591901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:33:39  来源:igfitidea点击:

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

javaandroidxmldependenciespreferences

提问by brybam

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

是否有与android:dependency?完全相反的 XML 属性?

What I would like the dependent preference to be enabled when the other is NOT checked and disabled when it IS checked.

我希望在未选中另一个时启用依赖首选项并在选中时禁用它。

edit: maybe the issue isn't with android:dependencymaybe there is an xml attribute that I can add to make the default for that preference disabled and then android:dependencywill toggle it the opposite way like i want.

编辑:也许问题不在于android:dependency也许我可以添加一个 xml 属性来禁用该首选项的默认值,然后android:dependency像我想要的那样以相反的方式切换它。

edit again: I tried setting android:enabled="false"in the preference and it disables it like i want but even with it being dependent on the other preference it didn't enable it like i had hoped

再次编辑:我尝试android:enabled="false"在首选项中进行设置,它像我想要的那样禁用它,但即使它依赖于其他首选项,它也没有像我希望的那样启用它

回答by brybam

Actually found it on my own and figured I'd just post it here to help anyone that might have this same issue:

实际上是我自己找到的,我想我只是把它贴在这里来帮助任何可能有同样问题的人:

android:disableDependentsState="true"

Put that in the controlling preference.

把它放在控制偏好中。

回答by Steve Waring

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.

Dmytro Zarezenko 问到,如果您希望某些依赖项在它们所依赖的首选项为真时启用,而某些依赖项在该首选项为假时启用,该怎么办。

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:

使用上述方法设置一种类型的所有依赖首选项(数字较大者)。然后(具有实现 OnSharedPreferenceChangeListener 的类)在 Preference Activity 和/或 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));
    }
}

回答by Justin

This is my code sample for doing this from code and not XML.

这是我从代码而不是 XML 执行此操作的代码示例。

  String eitherKey = "either";
  String orKey = "or";
  CheckBoxPreference either = new CheckBoxPreference(this);
  either.setKey(eitherKey);
  either.setTitle("Either");
  either.setSummary("It is either one or");
  either.setDefaultValue(false);
  either.setDisableDependentsState(true);
  inlinePrefCat.addPreference(either);

  try
  {
     //Crossfade Time
     CheckBoxPreference or = new CheckBoxPreference(this);
     or.setKey(orKey);
     or.setTitle("Or");
     or.setSummary("the other");
     inlinePrefCat.addPreference(or);
     or.setDependency(eitherKey);
  }
  catch (Exception e)
  {
  }

回答by davidm

I need to change value of dependent preference, so i post my code below, if anyone wants to do this:

我需要更改依赖偏好的值,所以我在下面发布我的代码,如果有人想这样做:

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    if(preference.getKey().equals("key_a")) {
        ((CheckBoxPreference)findPreference("key_b").setChecked(false);
    }
    return super.onPreferenceTreeClick(preferenceScreen, preference);
}

回答by Primo? Majhen

Make your PreferenceActivityimplement

做你的 PreferenceActivityimplement

SharedPreferences.OnSharedPreferenceChangeListener

SharedPreferences.OnSharedPreferenceChangeListener

declare in PreferenceActivity:

声明于PreferenceActivity

SharedPreferences prefs;

SharedPreferences prefs;

initialize in onCreate:

初始化onCreate

SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this); prefs = sPrefs;

SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this); prefs = sPrefs;

and register on shared preference change listener

并在共享首选项更改侦听器上注册

prefs.registerOnSharedPreferenceChangeListener(this);

do the same as Steve said in onResumeand onPausemethods.

像史蒂夫在onResumeonPause方法中所说的那样做。

implementation of onSharedPreferenceChangedlistener:

onSharedPreferenceChanged监听器的实现:

@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 Preferences in PreferenceActivity. Call and Record. When both are unchecked, both can be checked, but as user checks one of them, the other becomes disabled (greyed out). As user unchecks the checked preference, the user can check the other one.

在这种情况下,我在PreferenceActivity. 通话和录音。当两者都未选中时,两者都可以选中,但是当用户选中其中一个时,另一个将被禁用(变灰)。当用户取消选中选中的首选项时,用户可以选中另一个。

On both of them other preferences can depend and that can be worked out with android:dependancyattribute in XML file.

其他偏好可以依赖于它们两者,并且可以通过android:dependancyXML 文件中的属性来计算。