我的应用程序中有 Listpreferences。它们似乎没有在安装后立即设置为默认值 - 它们似乎为空。我试图弄清楚为什么在安装后没有立即设置我的默认首选项。在我的主要代码中,我有:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
InUnits = sp.getString("List1", "defValue");
InAngs = sp.getString("List2", "defValue");
OutUnits = sp.getString("List3", "defValue");
OutAngs = sp.getString("List4", "defValue");
在上面的代码执行之后,每个变量都包含“defValue”,而不是我在下面的 ListPreference 中分配的实际值。
我的偏好 xml 文件称为“settings.xml”。下面是其中一个 ListPreferences 的样子:
<ListPreference
android:key="List1"
android:title="Input: Alph"
android:summary="Choose Alph or Ralph"
android:entries="@array/inputAlph"
android:entryValues="@array/input_Alph_codes"
android:dialogTitle="Input Alph"
android:defaultValue="ININ"/>
这是我的一些 strings.xml 文件的样子:
<string-array name="inputUnits">
<item>Alph</item>
<item>Ralph</item>
</string-array>
<string-array name="input_Alph_codes">
<item>ININ</item>
<item>INMM</item>
</string-array>
当我进入菜单,然后是设置时,我可以看到我的默认设置已被选中(单选按钮)。然后,当我从设置菜单返回主屏幕时——一切都好——终生!...然后为上面的每个 var 分配适当的默认值。
这只发生在我第一次在手机上安装我的应用程序时。在我进入设置屏幕一次然后直接退出后,该应用程序很好并且可以接受任何设置更改。
顺便说一句,如您所见,“List1”是我的 res/xml 文件夹中名为 settings.xml 的文件中的 android:key。