java 在哪里存储 Android 首选项键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2857121/
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
Where to store Android preference keys?
提问by pixel
When I create preference activity I define all preferences in xml file. Every preference has a key defined in this xml. But when I access preference I write:
当我创建首选项活动时,我在 xml 文件中定义了所有首选项。每个首选项都有一个在此 xml 中定义的键。但是当我访问首选项时,我写道:
SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean foo_value = appPreferences.getBoolean("foo_key_defined_in_xml", false);
Is there any way to avoid referencing "foo_key_defined_in_xml" in hard-coded way? Maybe there is a possibility to reference it in R style way (not to refer to string)?
有什么方法可以避免以硬编码方式引用“foo_key_defined_in_xml”?也许有可能以 R 风格的方式引用它(而不是引用字符串)?
回答by pixel
I've found that it's possible to store keys in strings.xmland refer to them from preferences.xmljust like all other values android:key="@string/preference_enable".
我发现可以将键存储在strings.xml 中并从preferences.xml 中引用它们,就像所有其他值一样android:key="@string/preference_enable"。
In code you can refer to key by typing getString(R.string.preference_enable)
在代码中,您可以通过键入来引用键 getString(R.string.preference_enable)
You can mark the string to not be translated using a <xliff:g>tag. See Localization Checklist
您可以使用<xliff:g>标签将字符串标记为不翻译。查看本地化清单
<string name="preference_enable"><xliff:g id="preference_key">enable</xliff:g></string>
回答by Nico Pedraza
You could use a "keys.xml" files in "res/values", but should put something like this, this way you should dont have problem when you are using multiple languages:
您可以在“res/values”中使用“keys.xml”文件,但应该放置这样的内容,这样在使用多种语言时应该不会有问题:
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation">
<string name="key1">key1</string>
<string name="key2">key2</string>
...
</resources>
Then you could reference it like a normal string in xml:
然后你可以像 xml 中的普通字符串一样引用它:
....
android:key="@string/key1"
....
or in your java code for example:
或在您的 Java 代码中,例如:
SwitchPreference Pref1= (SwitchPreference) getPreferenceScreen().findPreference(getString(R.string.key1));
回答by Jeffrey
In strings.xml mark the key as untranslatable:
在 strings.xml 中将密钥标记为不可翻译:
<string name="screw_spacing_key" translatable="false">Screw spacing</string>
<string name="screw_spacing_title">Screw spacing</string>
<string name="screw_spacing_summary">Set screw spacing</string>
Usage:
用法:
<EditTextPreference
android:key="@string/screw_spacing_key"
android:title="@string/screw_spacing_title"
android:summary="@string/screw_spacing_summary"/>
See: Configure untranslatable rows
请参阅:配置不可翻译的行
回答by Adrian
As far as I know there's no better way of referencing preference keys (aside from maybe using a static final String to store the string on the class).
据我所知,没有更好的方法来引用首选项键(除了可能使用静态最终字符串将字符串存储在类中)。
The examplegiven in the SDK docs does the same as what you've given in your example,
该例子在SDK文档给出不一样的,你在你的例子已经给了什么,
回答by molnarm
Try getString(R.string.key_defined_in_xml).
试试getString(R.string.key_defined_in_xml)。
回答by Michael Hamilton
What about using a helper class to hide the getString() - instantiate the helper once in each activity or service. For example:
使用助手类隐藏 getString() 怎么样 - 在每个活动或服务中实例化一次助手。例如:
class Pref {
final String smsEnable_pref;
final String interval_pref;
final String sendTo_pref;
final String customTemplate_pref;
final String webUrl_pref;
Pref(Resources res) {
smsEnable_pref = res.getString(R.string.smsEnable_pref);
interval_pref = res.getString(R.string.interval_pref);
sendTo_pref = res.getString(R.string.sendTo_pref);
customTemplate_pref = res.getString(R.string.customTemplate_pref);
webUrl_pref = res.getString(R.string.webUrl_pref);
}
}
回答by Teddy Smith
Not sure if this post need another answer, put i end up to it like this:
不确定这篇文章是否需要另一个答案,我最终会这样:
-Extend all the Preference needed and add this code
-扩展所需的所有首选项并添加此代码
final static private int[] ATTR_INDEX = {android.R.attr.id};
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
if(attrs == null) return;
AttributeReader attributes = new AttributeReader().setAttrsIndex(ATTR_INDEX).parse(attrs);
int id = attributes.asResourceID(0);
setKey(AppContext.getIdentifierName(id));
}
- In the xml, don't use the key attribute but android:id="@+id/...
And finally to get back the value,
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getContext()); String preference_1 = SP.getString(AppContext.getIdentifierName(R.id.pref_key_1), null);
- 在xml中,不要使用key属性,而是使用android:id="@+id/...
最后为了取回价值,
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getContext()); 字符串首选项_1 = SP.getString(AppContext.getIdentifierName(R.id.pref_key_1), null);
This way, you don't have to create a string file who need to be maintain, just create any id on the fly. But you need to be familiar with extending a view and get read the attr to find what you want (Here the id attribute)
这样,您不必创建需要维护的字符串文件,只需动态创建任何 id。但是您需要熟悉扩展视图并阅读 attr 以找到您想要的内容(这里是 id 属性)

