Where to store Android preference keys?

前端 未结 7 889
孤城傲影
孤城傲影 2020-12-23 11:33

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:



        
相关标签:
7条回答
  • 2020-12-23 11:39

    Try getString(R.string.key_defined_in_xml).

    0 讨论(0)
  • 2020-12-23 11:40

    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,

    0 讨论(0)
  • 2020-12-23 11:49

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

    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)

    0 讨论(0)
  • 2020-12-23 11:54

    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>
    
    0 讨论(0)
  • 2020-12-23 11:59

    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));
    
    0 讨论(0)
  • 2020-12-23 12:00

    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

    0 讨论(0)
提交回复
热议问题