我有一个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"这样的东西
谢谢!
发布于 2010-11-10 04:02:22
要做到这一点,唯一的方法是编程。
您必须在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中),并添加/删除它,而不是启用/禁用。
这不是你见过的最好的答案吗?!
伊曼纽尔
发布于 2013-12-19 15:54:49
子类化ListPreference类,并覆盖setValue和shouldDisableDependence方法。
当该值实际发生更改时,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);
}发布于 2016-01-02 09:36:32
我试着编辑@waterdragon解决方案,但它被“同行拒绝”。不知道为什么,因为这仍然是他的解决方案,但提供了一个具体的例子。
子类化ListPreference类,并覆盖setValue和shouldDisableDependence方法。
当该值实际发生更改时,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" />https://stackoverflow.com/questions/3969807
复制相似问题