CheckBoxPreference中的值不会保留

时间:2013-11-06 20:19:27

标签: java android sharedpreferences checkboxpreference

我的xml文件中有以下首选项:

<?xml version="1.0" encoding="utf-8"?>
<CheckBoxPreference android:title="Alert" android:widgetLayout="@layout/preference_checkbox" android:key="alert"
    />

我想创建自己的自定义复选框,所以你可以看到上面我给出了这个偏好&#34; android:widgetLayout&#34;参数,链接到此文件:

<CheckBox 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/checkbox"
android:id="@+id/alert">

问题:使用(widgetLayout)参数,它不会保存复选框值。因此,如果我打开首选项CheckBox将被选中。然后,如果我取消选中它并再次打开首选项,则不存储之前的值,或者它不会保留。如果我使用CheckBoxPreference而不使用参数&#34; android:widgetLayou&#34;,在这种情况下会存储值,我就不会遇到这些问题。

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

您可以通过以下方式修复它:

1-将复选框ID更改为android:id =&#34; @android:id / checkbox

2-设置这3个attrs:

机器人:focusableInTouchMode =&#34;假&#34;

机器人:可点击=&#34;假&#34;

机器人:可聚焦=&#34;假&#34;

<CheckBox 
 android:id="@android:id/checkbox"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:button="@drawable/checkbox"
 android:clickable="false"
 android:focusable="false"
 android:focusableInTouchMode="false" >