我想创建一个偏好设置屏幕,在其中有三个复选框;第一个是可点击的,其他两个在第一个未被选中时不可用。
我该如何做?我看过这个教程,但只有一个复选框。有人能帮我吗?
我该如何做?我看过这个教程,但只有一个复选框。有人能帮我吗?
<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);
}
}
希望这可以帮到你。
您需要按照示例进行操作,但是您将有三个复选框
而不是一个。如果您希望在第一个复选框为真之前禁用两个复选框
,可以使用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>