ListPreference 依赖项

发布于 2024-09-27 19:01:18 字数 464 浏览 5 评论 0原文

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

<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"

谢谢!

I have a ListPreference which look something like this:

<ListPreference
android:title="Choose item"
android:summary="..."
android:key="itemList"
android:defaultValue="item1"
android:entries="@array/items"
android:entryValues="@array/itemValues" />

Then, I have another preference which should only be enabled if "item3" is selected in the ListPreference.

Can I somehow accomplish this with android:dependency? Something like android:dependency="itemList:item3"

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泛滥成性 2024-10-04 19:01:18

执行此类操作的唯一方法是以编程方式。

您必须在 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;
  }
});

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

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

以马内利

The only way you can do something like this is programmaticly.

You'd have to set up an change listener on the ListPreference and then enable/disable the other one.

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;
  }
});

If I were you I wouldn't even show the second preference if the first isn't set properly. To do that you have to declare the preference manually (not in the XML) and add/remove it instead of enabling/disabling.

Now isn't this the bestest answer you've ever seen?!

Emmanuel

风吹雨成花 2024-10-04 19:01:18

子类 ListPreference 类,并重写 setValueshouldDisableDependence 方法。

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

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

@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);
}

Subclass ListPreference class, and override setValue and shouldDisableDependence methods.

setValue will call notifyDependencyChange(shouldDisableDependents()) after super.setValue when the value is actually changed.

shouldDisableDependence returns false only if the current value is item3, or any other desired value stored in 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);
}
毁虫ゝ 2024-10-04 19:01:18

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

子类 ListPreference 类,并重写 setValueshouldDisableDependence 方法。

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

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

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:

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

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

并在您的首选项 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" />

I tried to edit @waterdragon solution but it was "peer rejected". Not sure why because it is still his solution but provides a concrete example.

Subclass ListPreference class, and override setValue and shouldDisableDependence methods.

setValue will call notifyDependencyChange(shouldDisableDependents()) after super.setValue when the value is actually changed.

shouldDisableDependence returns false only if the current value is item3, or any other desired value stored in 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);
    }
}

Update your attrs.xml:

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

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

and inside your preference xml tie it to any other preference that depends on it:

    <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" />
黑寡妇 2024-10-04 19:01:18

在 xml 中添加行

app:isPreferenceVisible="false"

然后将 onClickListener 添加到 java 中的 item3,在选择时将项目设置为 true

in the xml add the line

app:isPreferenceVisible="false"

Then add an onClickListener to item3 in the java that sets the item to true when selected

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文