根据其他首选项启用首选项

Boa*_*rdy 14 android android-preferences

我目前正在开发一个机器人应用程序,并想知道是否有一种方法可以根据另一个首选项的值启用或禁用首选项.

例如,如果我有checkbox_pref1,如果启用了这个,那么选项2/3/4被启用,如果checkbox_pref1被禁用,则option2/3/4会自动被禁用,或者这是否有一个XML属性可以完成此任务,或者它是我要做的事情需要编码来实现这种效果.

感谢您的任何帮助,您可以提供.

kco*_*ock 29

您可以使用该dependency属性执行此操作.在你的PreferenceXML,你将下面的行添加到您checkbox_pref2,checkbox_pref3checkbox_pref2首选项:

android:dependency="checkbox_pref1"
Run Code Online (Sandbox Code Playgroud)

有用的链接:

Android快速首选项教程

Android首选项文档(#dependency)


小智 7

这是我的禁用PreferenceScreen的示例,直到选中主复选框首选项为止 - 谢谢kcoppock!对不起,我的代表不够高兴你.

        <CheckBoxPreference
            android:title="@string/Day1_title"
            android:summary="@string/Day1_summary"
            android:key="pref_Day1" />
        <PreferenceScreen
            android:dependency="pref_Day1"
            android:key="day1_screen"
            android:summary="@string/Extra_Options">
            <EditTextPreference
              android:title="@string/Day1_title"
              android:summary="@string/Workout_Date"
              android:key="Day1_date"/>
            <EditTextPreference
              android:title="@string/Comment_title"
              android:key="Day1_comment"/>
        </PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)