我认为将首选项保存在prefs.xml中没有任何问题。在重新创建你的代码并稍微修改它之后,我不得不怀疑普通的<Preference>-Tag代替:
字符串.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TestPreference</string>
<string name="action_settings">Settings</string>
<string name="pref_header_1">Preference Category</string>
<string name="pref_default_1">Image 1</string>
<string name="pref_default_2">Image 2</string>
<string name="pref_title_1">Title 1</string>
<string name="pref_title_2">Title 2</string>
<string name="pref_title_3">Title 3</string>
<string name="pref_key_1">pref_key_1</string>
<string name="pref_key_2">pref_key_2</string>
<string name="pref_key_3">pref_key_3</string>
<string-array name="pref_entries_3">
<item>One</item>
<item>Two</item>
<item>Five</item>
</string-array>
<string-array name="pref_values_3">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>
偏好.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/pref_header_1" >
<Preference
android:defaultValue="@string/pref_default_1"
android:key="@string/pref_key_1"
android:title="@string/pref_title_1" />
<EditTextPreference
android:defaultValue="@string/pref_default_2"
android:key="@string/pref_key_2"
android:title="@string/pref_title_2" />
<ListPreference
android:defaultValue="1"
android:entries="@array/pref_entries_3"
android:entryValues="@array/pref_values_3"
android:key="@string/pref_key_3"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_3" />
</PreferenceCategory>
</PreferenceScreen>
MainActivity.java
private Logger log = LoggerFactory.getLogger(getClass());
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get default preferences and load default values
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
// Check if keys exist in sharedPreferences
log.debug("Contains keys #1: {}; #2: {}; #3: {}", new Object[] {
prefs.contains(getString(R.string.pref_key_1)),
prefs.contains(getString(R.string.pref_key_2)),
prefs.contains(getString(R.string.pref_key_3))
});
// Get preferences values
String value1 = prefs.getString(getString(R.string.pref_key_1), "not found");
String value2 = prefs.getString(getString(R.string.pref_key_2), "not found");
String value3 = prefs.getString(getString(R.string.pref_key_3), "not found");
log.debug("Values #1: {}; #2: {}; #3: {}", new Object[] { value1, value2, value3 });
// prefs.edit().putString(getString(R.string.pref_key_1), getString(R.string.pref_default_1)).apply();
}
记录结果
"Contains keys #1: false; #2: true; #3: true"
"Values #1: not found; #2: Image 2; #3: 1"
结论
如果您希望某个首选项是用户可编辑的,您可能最好使用具体的首选项,例如 EditTextPreference、ListPreference、CheckboxPreferencete(在此处查看适合您需要的子类的完整列表。
如果出于某种原因您仍然想要要使用普通首选项,我想您必须像这样手动加载它们:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString(getString(R.string.pref_key_1), getString(R.string.pref_default_1)).apply();