java Android 从preferences.xml 获取密钥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5246741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Get Keys from preferences.xml
提问by liquid
I have a PreferencesActivity
that shows a preferences.xml
with checkboxes.
我有一个PreferencesActivity
显示preferences.xml
带有复选框的。
preferences.xml
:
preferences.xml
:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Keywords">
<CheckBoxPreference android:key="Essen"
android:title="Essen"
android:selectable="true"
android:enabled="true"
android:persistent="false">
</CheckBoxPreference>
<CheckBoxPreference android:key="Kleidung"
android:title="Kleidung"
android:selectable="true"
android:enabled="true"
android:persistent="false">
</CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
PreferencesActivity:
首选项活动:
public class PreferencesViewController extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Now in a different ListActivity
I want to show all Keys/Titles from the checked checkboxes.
现在,ListActivity
我想显示选中复选框中的所有键/标题。
I try to access the Preferences with
我尝试访问首选项
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
or
或者
SharedPreferences prefs = getSharedPreferences("mypackage_preferences", 0);
But both dont really work.
但两者都不起作用。
When I call prefs.getAll().size()
the result is 0.
当我打电话时prefs.getAll().size()
,结果是 0。
I can access the Keys/Title with getPreferenceScreen().getPreference(i).…
but it doesn't work from a different Activity
, only from the PreferenceActivity
.
我可以访问 Keys/Title ,getPreferenceScreen().getPreference(i).…
但它不能从不同Activity
的PreferenceActivity
.
Does anybody have a solution how to get this work?
有没有人有解决方案如何获得这项工作?
回答by rekaszeru
You don't need a PreferenceActivity
to accomplish this.
你不需要一个PreferenceActivity
来完成这个。
To access the preferences, that are used in your PreferenceActivity, you should use
要访问在 PreferenceActivity 中使用的首选项,您应该使用
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
If your list of data comes from the server, than you can use a ListActivity / ExpandableListActivity / any custom activity to visualize it, but this way you need to write the handlers that change the preferences.
如果您的数据列表来自服务器,那么您可以使用 ListActivity / ExpandableListActivity / 任何自定义活动来对其进行可视化,但通过这种方式,您需要编写更改首选项的处理程序。
The common way to do this would be:
执行此操作的常用方法是:
private void saveStringPreference(final String key, final String value)
{
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
If you need, you should create similar wrappers to deal with int, boolean, etc. values.
如果需要,您应该创建类似的包装器来处理 int、boolean 等值。
回答by Ridcully
I use
我用
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
And then I can access the preferences via prefs.get...()
, e.g. prefs.getString(key)
.
Have you tried this?
然后我可以通过prefs.get...()
例如访问首选项prefs.getString(key)
。你试过这个吗?
Edit:Just checked - prefs.getAll()
works as expected and returns a Map
with all preferences.
编辑:刚刚检查 -prefs.getAll()
按预期工作并返回Map
所有首选项。
回答by Victor Ionescu
I had this problem too. This is a classic RTFM, sadly. You should put the line below in your MainActivity's onCreate() method:
我也有这个问题。遗憾的是,这是一个经典的 RTFM。您应该将以下行放在 MainActivity 的 onCreate() 方法中:
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
This info can be found in the paragraph "Setting Default Values" of http://developer.android.com/guide/topics/ui/settings.html
此信息可以在http://developer.android.com/guide/topics/ui/settings.html 的“设置默认值”段落中找到
回答by Robby Pond
You should use
你应该使用
SharedPreferences prefs = referenceManager.getDefaultSharedPreferences(this);
The preferences may be empty if you have never set them via the PreferencesActivity. Also I think your ListActivity has to be in the same package as the PreferencesActivity.
如果您从未通过 PreferencesActivity 设置首选项,则首选项可能为空。另外我认为您的 ListActivity 必须与 PreferencesActivity 在同一个包中。