我在共享首选项中创建列表,当调用 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 不起作用。我知道如何使用字符串数组而不是值数组来做到这一点。而且我可以提取值字符串并从中得出我想要的东西,但我宁愿(如果可能的话)能够拥有值是整数、布尔值或枚举的列表。这样做的习惯方法是什么?
提前致谢,
周杰伦