Where to store Android preference keys?

2019-01-30 17:24发布

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:

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)?

6条回答
来,给爷笑一个
2楼-- · 2019-01-30 17:58

Try getString(R.string.key_defined_in_xml).

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-30 18:06

In strings.xml mark the key as untranslatable:

<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

查看更多
戒情不戒烟
4楼-- · 2019-01-30 18:08

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 example given in the SDK docs does the same as what you've given in your example,

查看更多
疯言疯语
5楼-- · 2019-01-30 18:10

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:

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

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

or in your java code for example:

SwitchPreference Pref1= (SwitchPreference) getPreferenceScreen().findPreference(getString(R.string.key1));
查看更多
乱世女痞
6楼-- · 2019-01-30 18:17

I've found that it's possible to store keys in strings.xml and refer to them from preferences.xml just like all other values android:key="@string/preference_enable".

In code you can refer to key by typing getString(R.string.preference_enable)

You can mark the string to not be translated using a <xliff:g> tag. See Localization Checklist

<string name="preference_enable"><xliff:g id="preference_key">enable</xliff:g></string>
查看更多
做个烂人
7楼-- · 2019-01-30 18:24

What about using a helper class to hide the getString() - instantiate the helper once in each activity or service. For example:

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);
    }
}
查看更多
登录 后发表回答