ListPreference依赖项

shu*_*uwo 26 dependencies android preferences

我有一个ListPreference看起来像这样的东西:

<ListPreference
android:title="Choose item"
android:summary="..."
android:key="itemList"
android:defaultValue="item1"
android:entries="@array/items"
android:entryValues="@array/itemValues" />
Run Code Online (Sandbox Code Playgroud)

然后,我有另一个偏好,只有在选择"item3"时才应启用ListPreference.

我能以某种方式实现这一目标android:dependency吗?就像是android:dependency="itemList:item3"

谢谢!

Emm*_*uel 29

你可以做这样的事情的唯一方法是程序化.

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

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;
  }
});
Run Code Online (Sandbox Code Playgroud)

如果我是你,如果没有正确设置,我甚至不会显示第二个偏好.要做到这一点,您必须手动声明首选项(而不是在XML中)并添加/删除它而不是启用/禁用.

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

灵光

  • 我会站出来说这是我见过的最好的答案. (5认同)
  • @Emmanuel:itemList和itemList2变量应该声明为final.无论如何,我投了票,因为你的答案对我很有用! (3认同)

小智 7

ListPreference类,替换setValueshouldDisableDependence方法.

setValuenotifyDependencyChange(shouldDisableDependents())super.setValue实际更改值后调用.

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

@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);
}
Run Code Online (Sandbox Code Playgroud)


She*_*ude 5

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

ListPreference类,以及覆盖setValueshouldDisableDependence方法。

setValue将在值实际更改notifyDependencyChange(shouldDisableDependents())后调用super.setValue

shouldDisableDependence仅当当前值为 item3 或存储在mDepedenceValue.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

更新您的 attrs.xml:

<attr name="dependentValue" format="string" />

<declare-styleable name="DependentListPreference">
    <attr name="dependentValue" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

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

    <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" />
Run Code Online (Sandbox Code Playgroud)