java - 不保留 CheckBoxPreference 中的值

标签 java android sharedpreferences checkboxpreference

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

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

我想制作自己的自定义复选框,因此正如您在上面看到的,我提供了此首选项“android:widgetLayout”参数,该参数链接到此文件:

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

问题:使用 (widgetLayout) 参数时,它不会保存复选框值。因此,如果我打开首选项复选框就会被选中。然后,如果我取消选中它,并再次打开首选项,则之前的值不会存储,或者不会保留。如果我使用不带参数“android:widgetLayou”的 CheckBoxPreference,则不会出现此问题,在本例中,值已存储。

有什么想法吗?

最佳答案

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

1-将复选框 ID 更改为 android:id="@android:id/checkbox

2- 设置这 3 个属性:

android:focusableInTouchMode="false"

android:clickable="false"

android:focusable="false"

<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" >

关于java - 不保留 CheckBoxPreference 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821979/

相关文章:

java - 转换到按钮控件(Android): what exactly is that?

java - wait.until(ExpectedConditions.visibilityOf Element1 或 Element2)

android - 图片未显示在 ScrollView 中的android imageview中

java - android中listview的动态定位

android - 在同一应用程序的 PreferenceActivity 和 Service 中管理 SharedPreferences

java - 重新启动后无法访问另一个类中的 SharedPreferences 值

java - 查找在多个 servlet 映射场景中使用的匹配 url 模式

java - 使用 AES 缓存用户密码 -- BadPaddingException

java - 以编程方式访问Manifest.permission?

关于 PreferenceActivity 的 Android 弃用方法警告