63

当我创建首选项活动时,我在 xml 文件中定义所有首选项。每个首选项都有一个在此 xml 中定义的键。但是当我访问首选项时,我会写:

SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean foo_value = appPreferences.getBoolean("foo_key_defined_in_xml", false);

有什么方法可以避免以硬编码方式引用“foo_key_defined_in_xml”?也许有可能以 R 风格的方式引用它(而不是引用字符串)?

4

7 回答 7

75

我发现可以将键存储在strings.xml中,并像所有其他值一样从preferences.xml中引用它们android:key="@string/preference_enable"

在代码中,您可以通过键入来引用密钥 getString(R.string.preference_enable)

您可以使用标签将字符串标记为不翻译<xliff:g>。请参阅本地化清单

<string name="preference_enable"><xliff:g id="preference_key">enable</xliff:g></string>
于 2010-05-19T09:10:50.090 回答
15

您可以在“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>

然后你可以像 xml 中的普通字符串一样引用它:

....
android:key="@string/key1"
....

或在您的 java 代码中,例如:

SwitchPreference Pref1= (SwitchPreference) getPreferenceScreen().findPreference(getString(R.string.key1));
于 2016-03-27T03:54:13.533 回答
4

在 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>

用法:

<EditTextPreference
    android:key="@string/screw_spacing_key"
    android:title="@string/screw_spacing_title"
    android:summary="@string/screw_spacing_summary"/>

请参阅:配置不可翻译的行

于 2018-09-25T00:23:32.373 回答
1

据我所知,没有更好的方法来引用首选项键(除了可能使用静态最终字符串将字符串存储在类中)。

SDK 文档中给出的示例与您在示例中给出的示例相同,

于 2010-05-18T12:33:44.650 回答
1

试试getString(R.string.key_defined_in_xml)

于 2010-05-18T12:37:03.483 回答
1

使用帮助器类来隐藏 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);
    }
}
于 2011-02-09T10:38:55.753 回答
0

不确定这篇文章是否需要另一个答案,让我这样结束:

- 扩展所需的所有首选项并添加此代码

   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));
    }
  • 在 xml 中,不要使用 key 属性,而是使用 android:id="@+id/...
  • 最后为了取回价值,

    SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getContext()); 字符串偏好_1 = SP.getString(AppContext.getIdentifierName(R.id.pref_key_1), null);

这样,您不必创建需要维护的字符串文件,只需动态创建任何 id。但是您需要熟悉扩展视图并阅读 attr 以找到您想要的(这里是 id 属性)

于 2020-05-09T06:05:52.413 回答