使CheckBoxPreference在SharedPreferences中存储字符串(“ true” /“ false”)而不是布尔值

乔斯·D(JoséD.)

我有一个PreferentFragment,其内容是从包含CheckBoxPreference的.xml文件中加载的

    <CheckBoxPreference
          android:title="@string/pref_secure_conn"
          android:key="enable_secure_connection"
          android:defaultValue="false"/>

如您所知,当用户与此首选项交互时,SharedPreferences对象会自动更新,以便android:key现在包含正确的布尔值。

但是,我想使用String而不是Boolean:是否有任何方法可以使CheckBoxPreference使用String值而不是Boolean值,以便以后可以使用该键调用getString?

目前,我只是在听'onSharedPreferenceChanged'并手动更改它,但是也许有更好的方法。(另一个明显的解决方法是在需要此值时使用getBoolean而不是getString,但让我们假设我无法做到这一点)

搅拌机

我想到的最简单的方法是继承CheckBoxPreference并保存Preference冗余:

package com.example.preferencestest;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;

public class StringCheckBoxPreference extends CheckBoxPreference {
    public StringCheckBoxPreference(Context context) { super(context); }
    public StringCheckBoxPreference(Context context, AttributeSet attrs) { super(context, attrs); }
    public StringCheckBoxPreference(Context context, AttributeSet attrs,
            int defStyle) { super(context, attrs, defStyle); }

    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);
        SharedPreferences p = getSharedPreferences();
        SharedPreferences.Editor e = p.edit();
        e.putString(getKey()+"asString", String.valueOf(checked));
        e.apply();
    }
}

您可以将此类添加到您的PreferencesActivity xml中,如下所示:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <com.example.preferencestest.StringCheckBoxPreference
        android:defaultValue="true"
        android:key="myCheckBox"
        android:summary="@string/pref_description_social_recommendations"
        android:title="@string/pref_title_social_recommendations"
    />

    <CheckBoxPreference
        android:defaultValue="true"
        android:key="example_checkbox"
        android:summary="@string/pref_description_social_recommendations"
        android:title="@string/pref_title_social_recommendations" />

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

熊猫以字符串而不是布尔值映射到TRUE / FALSE

我怎么能在Python的集合中添加布尔值False而不是True?

Laravel布尔值在查询中返回“ 1” /“ 0”,而不是true / false

在Gson中,如何将布尔值限制为true和false(不是TrUe和fAlSe)?

TRUE和FALSE为布尔值,与字符相同

计算列中布尔值从True变为False的次数

在PHP中获取随机布尔值true / false

将布尔值[True,False,False ...]的SEQUENCE更改为位字符串序列

字符串连接将True视为布尔值而不是字符串

将字符串“ true” /“ false”转换为布尔值

如何在python JSON字符串处转义true / false布尔值

Coq中命题True / False和布尔值True / False之间的区别

空字符串的XSLT布尔值输出true

Python的in(__contains__)运算符返回布尔值,该布尔值既不是True也不是False

用它们各自的布尔值替换 json 中的所有“TRUE”和“FALSE”值

如何在angular中使用布尔值true或false禁用ngClass中的div

R:从一组布尔值中返回一个TRUE / FALSE

为什么Java中的布尔值只接受true或false?为什么也不要1或0?

在JavaScript中,我似乎无法针对布尔值是否为True / False做出if语句

对于x-www-form-urlencoded,NSDictionary中的布尔值序列化为true / false

布尔值(True和False)在Python概念和shell概念中的价值是什么?

如何在Java>中检查多重布尔值true或false

如何在 cJSON 中测试布尔值是否为 TRUE/FALSE

Clojure布尔值true或false

重命名true false布尔值

布尔值证明,false = true

为什么“ 0 && true”在JavaScript中返回0而不是布尔值?

Scala - 返回一个既不是 True 也不是 False 的布尔值

我正在尝试从类中的布尔值返回字符串,并始终返回true