首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ListPreference依赖

ListPreference依赖
EN

Stack Overflow用户
提问于 2010-10-19 23:06:43
回答 4查看 12.1K关注 0票数 28

我有一个ListPreference,它看起来像这样:

代码语言:javascript
复制
<ListPreference
android:title="Choose item"
android:summary="..."
android:key="itemList"
android:defaultValue="item1"
android:entries="@array/items"
android:entryValues="@array/itemValues" />

然后,我还有另一个首选项,只有在ListPreference中选择了"item3“时才应该启用。

我能用android:dependency以某种方式实现这一点吗?像android:dependency="itemList:item3"这样的东西

谢谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-11-10 04:02:22

要做到这一点,唯一的方法是编程。

您必须在ListPreference上设置一个更改监听器,然后启用/禁用另一个监听器。

代码语言:javascript
复制
itemList = (ListPreference)findPreference("itemList");
itemList2 = (ListPreference)findPreference("itemList2");
itemList.setOnPreferenceChangeListener(new
Preference.OnPreferenceChangeListener() {
  public boolean onPreferenceChange(Preference preference, Object newValue) {
    final String val = newValue.toString();
    int index = itemList.findIndexOfValue(val);
    if(index==3)
      itemList2.setEnabled(true);
    else
      itemList2.setEnabled(false);
    return true;
  }
});

如果我是你,如果第一个参数设置不正确,我甚至不会显示第二个参数。为此,您必须手动声明首选项(而不是在XML中),并添加/删除它,而不是启用/禁用。

这不是你见过的最好的答案吗?!

伊曼纽尔

票数 29
EN

Stack Overflow用户

发布于 2013-12-19 15:54:49

子类化ListPreference类,并覆盖setValueshouldDisableDependence方法。

当该值实际发生更改时,setValue将在super.setValue之后调用notifyDependencyChange(shouldDisableDependents())

仅当当前值为item3或存储在mDepedenceValue中的任何其他所需值时,shouldDisableDependence才返回false。

代码语言:javascript
复制
@Override
public void setValue(String value) {
    String mOldValue = getValue();
    super.setValue(value);
    if (!value.equals(mOldValue)) {
        notifyDependencyChange(shouldDisableDependents());
    }
}

@Override
public boolean shouldDisableDependents() {
    boolean shouldDisableDependents = super.shouldDisableDependents();
    String value = getValue();
    return shouldDisableDependents || value == null || !value.equals(mDepedenceValue);
}
票数 7
EN

Stack Overflow用户

发布于 2016-01-02 09:36:32

我试着编辑@waterdragon解决方案,但它被“同行拒绝”。不知道为什么,因为这仍然是他的解决方案,但提供了一个具体的例子。

子类化ListPreference类,并覆盖setValueshouldDisableDependence方法。

当该值实际发生更改时,setValue将在super.setValue之后调用notifyDependencyChange(shouldDisableDependents())

仅当当前值为item3或存储在mDepedenceValue中的任何其他所需值时,shouldDisableDependence才返回false。

代码语言:javascript
复制
package xxx;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.ListPreference;
import android.util.AttributeSet;

import xxx.R;

public class DependentListPreference extends ListPreference {

    private final String CLASS_NAME = this.getClass().getSimpleName();
    private String dependentValue = "";

    public DependentListPreference(Context context) {
        this(context, null);
    }
    public DependentListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DependentListPreference);
            dependentValue = a.getString(R.styleable.DependentListPreference_dependentValue);
            a.recycle();
        }
    }

    @Override
    public void setValue(String value) {
        String mOldValue = getValue();
        super.setValue(value);
        if (!value.equals(mOldValue)) {
            notifyDependencyChange(shouldDisableDependents());
        }
    }

    @Override
    public boolean shouldDisableDependents() {
        boolean shouldDisableDependents = super.shouldDisableDependents();
        String value = getValue();
        return shouldDisableDependents || value == null || !value.equals(dependentValue);
    }
}

更新您的attrs.xml:

代码语言:javascript
复制
<attr name="dependentValue" format="string" />

<declare-styleable name="DependentListPreference">
    <attr name="dependentValue" />
</declare-styleable>

在您的首选项xml中,将其与依赖于它的任何其他首选项绑定:

代码语言:javascript
复制
    <xxx.DependentListPreference
        android:key="pref_key_wifi_security_type"
        android:title="Wi-Fi Security Type"
        app:dependentValue="1"
        android:entries="@array/wifi_security_items"
        android:entryValues="@array/wifi_security_values" />

    <EditTextPreference
        android:key="pref_key_wifi_security_key"
        android:title="WPA2 Security Key"
        android:summary="Tap to set a security key"
        android:password="true"
        android:dependency="pref_key_wifi_security_type" />
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3969807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档