我在共享首选项中创建列表,并且在调用onPreferenceChanged()方法时,我想提取列表中项目的索引或在某些情况下提取整数值。我试图建立xml数据,如下所示:

在数组中:

<string-array name="BackgroundChoices">
  <item>Dark Background</item>
  <item>Light Background</item>
</string-array>
<array name="BackgroundValues">
  <item>1</item>
  <item>0</item>
</array>
<string-array name="SpeedChoices">
  <item>Slow</item>
  <item>Medium</item>
  <item>Fast</item>
</string-array>
<array name="SpeedValues">
  <item>1</item>
  <item>4</item>
  <item>16</item>
</array>

在首选项xml文件中:
<PreferenceScreen android:key="Settings"
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:title="Settings">

<ListPreference
        android:key="keyBackground"
        android:entries="@array/BackgroundChoices"
        android:summary="Select a light or dark background."
        android:title="Light or Dark Background"
        android:positiveButtonText="Okay"
        android:entryValues="@array/BackgroundValues"/>
<ListPreference
        android:key="keySpeed"
        android:entries="@array/SpeedChoices"
        android:summary="Select animation speed."
        android:title="Speed" android:entryValues="@array/SpeedValues"/>
</PreferenceScreen>

所以我的xml不起作用。我知道如何使用字符串数组而不是值数组。而且我可以拉出值字符串并从中得出我想要的东西,但我希望(如果可能的话)能够列出值是整数, bool(boolean) 值或枚举的列表。习惯的做法是什么?

提前致谢,

周杰伦

最佳答案

将首选项设置为String并使用Integer.parseInt()。我认为实际上有一个有关您所指的限制的错误报告,但是我找不到链接。根据经验,我可以告诉您只使用Strings并为自己省去很多麻烦。

请注意其他SO用户,如果可以证明我做错了,我欢迎您。

07-24 09:49
查看更多