加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何获取Xml中定义的值为整数的Android ListPreference?

发布时间:2020-12-16 08:02:35 所属栏目:百科 来源:网络整理
导读:可以在Xml中定义ListPreference,并使用getInt从SharedPreferences中检索值吗?这是我的Xml: ListPreference android:key="@string/prefGestureAccuracyKey" android:title="@string/prefGestureAccuracyTitle" android:summary="@string/prefGestureAccura
可以在Xml中定义ListPreference,并使用getInt从SharedPreferences中检索值吗?这是我的Xml:
<ListPreference android:key="@string/prefGestureAccuracyKey"
    android:title="@string/prefGestureAccuracyTitle" android:summary="@string/prefGestureAccuracyDesc"
    android:entries="@array/prefNumberAccuracyLabels" android:entryValues="@array/prefNumberAccuracyValues"
    android:dialogTitle="@string/prefGestureAccuracyDialog"
    android:persistent="true" android:defaultValue="2"
    android:shouldDisableView="false" />

我想要获得的值如下:int val = sharedPrefs.getInt(key,defaultValue)。

目前我必须使用getString并解析结果。

我的理解是ListPreference只能用于字符串数组。你必须使用getString()并自己转换为整数。请参阅 http://code.google.com/p/android/issues/detail?id=2096的错误报告。它似乎不像Google计划扩展ListPreference来处理除字符串之外的任何东西。

另外:您需要将首选项存储为字符串。否则,您的首选项活动将在启动时崩溃,并尝试读取字符串值。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读