ListPreference 依赖项
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行此类操作的唯一方法是以编程方式。
您必须在 ListPreference 上设置一个更改侦听器,然后启用/禁用另一个侦听器。
如果我是你,如果第一个设置不正确,我什至不会显示第二个偏好。为此,您必须手动声明首选项(不在 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.
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
子类
ListPreference
类,并重写setValue
和shouldDisableDependence
方法。当值实际更改时,
setValue
将在super.setValue
之后调用notifyDependencyChange(shouldDisableDependents())
。仅当当前值为 item3 或
mDepedenceValue
中存储的任何其他所需值时,shouldDisableDependence
才会返回 false。Subclass
ListPreference
class, and overridesetValue
andshouldDisableDependence
methods.setValue
will callnotifyDependencyChange(shouldDisableDependents())
aftersuper.setValue
when the value is actually changed.shouldDisableDependence
returns false only if the current value is item3, or any other desired value stored inmDepedenceValue
.我尝试编辑@waterdragon 解决方案,但它被“同行拒绝”。不知道为什么,因为这仍然是他的解决方案,但提供了一个具体的例子。
子类
ListPreference
类,并重写setValue
和shouldDisableDependence
方法。当值实际更改时,
setValue
将在super.setValue
之后调用notifyDependencyChange(shouldDisableDependents())
。仅当当前值为 item3 或
mDepedenceValue
中存储的任何其他所需值时,shouldDisableDependence
才会返回 false。更新您的 attrs.xml:
并在您的首选项 xml 中将其与依赖于它的任何其他首选项相关联:
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 overridesetValue
andshouldDisableDependence
methods.setValue
will callnotifyDependencyChange(shouldDisableDependents())
aftersuper.setValue
when the value is actually changed.shouldDisableDependence
returns false only if the current value is item3, or any other desired value stored inmDepedenceValue
.Update your attrs.xml:
and inside your preference xml tie it to any other preference that depends on it:
在 xml 中添加行
然后将 onClickListener 添加到 java 中的 item3,在选择时将项目设置为 true
in the xml add the line
Then add an onClickListener to item3 in the java that sets the item to true when selected