如何获得在XML中,它们的值是整数定义一个Android目录preference?它们的、整数、如何获得、定义

2023-09-05 05:31:13 作者:因太萌被开除

是否有可能定义一个列表preference XML和检索使用调用getInt共享preferences的价值?这里是我的XML:

Is it possible to define a ListPreference in Xml and retrieve the value from SharedPreferences using getInt? Here is my 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 =共享prefs.getInt(键,设置defaultValue)

And I want to get the value with something like: int val = sharedPrefs.getInt(key, defaultValue).

目前,我必须使用的getString和分析的结果。

At the moment I have to use getString and parse the result.

推荐答案

我的理解是,清单preference只能与字符串数组的工作。你将不得不使用的getString(),并转换为整数自己。请参见 HTTP://$c$c.google.com/ P /安卓/问题/详细信息?ID = 2096 了解这个bug报告。它看起来并不像谷歌计划扩大列表preference处理任何事情,但字符串。

My understanding is that ListPreference can only work with string arrays. You'll have to use getString() and convert to integer yourself. See http://code.google.com/p/android/issues/detail?id=2096 for the bug report on this. It doesn't look like Google plans to extend ListPreference to handle anything but strings.

另外:你需要保存preference作为一个字符串了。否则,当它启动并尝试读取一个字符串值,您的preferences活动将崩溃。

Also: You'll need to store the preference as a string too. Otherwise, your preferences activity will crash when it starts up and tries to read a string value.