我不明白为什么我的复选框偏好设置未保存或未正确读取。我在AndroidManifest.xml中有以下代码:
<activity android:name=".Preferences" />
然后在res / xml / preferences.xml
中<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Preferences">
<CheckBoxPreference
android:title="Always Run"
android:key="@string/pref_always_run_key"
android:summary="Always run it" />
</PreferenceCategory>
在res / values / strings.xml
中<string name="pref_always_run_key">always_run_default</string>
然后我有一个src / com.name / Preferences.java文件
public class Preferences extends PreferenceActivity
{
private static final String LOG_TAG = Preferences.class.getSimpleName() + "_LOG";
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences); //5
//getPreferenceManager().setSharedPreferencesName();
CheckBoxPreference alwaysRunCheckBox = (CheckBoxPreference)findPreference(getString(R.string.pref_always_run_key));// 8
alwaysRunCheckBox.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
{
@Override
public boolean onPreferenceChange(Preference preference, Object newValue)
{
Log.d(LOG_TAG, "User set always run to " + newValue.toString().equals("true"));
return true;
}
});
}
}
然后我在MainActivity.java中有一个测试按钮
SharedPreferences prefs = MainActivity.this.getSharedPreferences("com.projname.Preferences", Context.MODE_PRIVATE);
String defaultAlwaysRunKey = MainActivity.this.getResources().getString(R.string.pref_always_run_key);
Log.d(LOG_TAG, "Always run key is " + defaultAlwaysRunKey);
boolean run = prefs.getBoolean(defaultAlwaysRunKey, false);
Log.d(LOG_TAG, "It contains the key: " + prefs.contains(defaultAlwaysRunKey));
Log.d(LOG_TAG, "Always run set to " + run);
Log.d(LOG_TAG, "All the preferences saved are: " + prefs.getAll().toString());
输出始终是:
10-25 16:15:50.844: D/MainActivity_LOG(1219): Clicked on test...
10-25 16:15:50.844: D/MainActivity_LOG(1219): Always run key is always_run_default
10-25 16:15:50.844: D/MainActivity_LOG(1219): It contains the key: false
10-25 16:15:50.844: D/MainActivity_LOG(1219): Always run set to false
10-25 16:15:50.855: D/MainActivity_LOG(1219): All the preferences saved are: {}
无论是否选中该复选框。
答案 0 :(得分:4)
您忘记使用SharedPreference的编辑器并调用编辑器对象的commit();
方法。
CheckBoxPreference alwaysRunCheckBox = (CheckBoxPreference)findPreference(getString(R.string.pref_always_run_key));// 8
alwaysRunCheckBox.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
{
@Override
public boolean onPreferenceChange(Preference preference, Object newValue)
{
boolean val = Boolean.parseBoolean(newValue.toString());
Editor e = preference.getEditor();
e.putBoolean(getString(R.string.pref_always_run_key), val );
e.commit();
Log.d(LOG_TAG, "User set always run to " + String.valueOf(val));
return true;
}
});
答案 1 :(得分:4)
我认为您的问题是您从不同的首选项文件中读取而不是您写入的首选项文件。您按名称阅读,但写入默认值。
尝试将MainActivity分配更改为
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(MainActivity.this);