如何创建带有复选框Android的设置菜单



我想创建一个首选项屏幕,其中有三个复选框;第一个是可点击的,另外两个直到第一个被选中才可点击。

我该怎么做?我看过这个教程,但是只有一个复选框。有人能帮我吗?

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
     <PreferenceCategory
           android:summary="@string/summary_category"
           android:title="@string/title_category">
           <CheckBoxPreference
                 android:key="main"
                 android:defaultValue="true"
                 android:summary="@string/summary_main"
                 android:title="@string/title_main" 
          />
          <CheckBoxPreference
                android:key="firstDependent"
                android:summary="@string/summary_firstDependent"
                android:title="@string/title_firstDependent"
                android:dependancy="main"
          />
          <CheckBoxPreference
                android:key="secondDependent"
                android:summary="@string/summary_secondDependent"
                android:title="@string/title_secondDependent"
                android:dependancy="main"
          />
    </PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>

您只需将android:dependancy设置为相应复选框必须依赖的复选框的键即可完成此操作。

现在在res文件夹中创建一个名为xml的文件夹,并将您的首选项xml文件放入其中。然后执行以下操作。

public class SettingsActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

    }

}    

你也可以用更推荐的碎片来做这件事。但上述方法要简单得多。如果您想对片段执行此操作,请选中此项,其中包含有关创建"设置活动"的所有信息。

希望这能有所帮助

您必须像在那个例子中那样做,但您将有三个checkboxes而不是一个。如果希望禁用两个checkboxes,直到第一个为true,则可以使用android:dependency属性。使用此属性,您需要指定它们所依赖的首选项的

<PreferenceCategory
    android:summary="..."
    android:title="..." >
    <CheckBoxPreference
        android:defaultValue="true"
        android:key="first"
        android:summary="@string/summary_first"
        android:title="@string/title_first" />
    <CheckBoxPreference
        android:defaultValue="false"
        android:dependency="first"
        android:key="second"
        android:summary="@string/summary_second"
        android:title="@string/title_second" />
    <CheckBoxPreference
        android:defaultValue="false"
        android:dependency="first"
        android:key="third"
        android:summary="@string/summary_third"
        android:title="@string/title_third" />
</PreferenceCategory>

最新更新