Preference Activity Preference Settings

The following is an introduction to the components in Preference Activity.

  • PreferenceScreen: Setting pages can be nested to form secondary settings pages and titles can be set with Title parameters (this can also be used as a separate activity to start the settings interface, not yet studied).

    1. PreferenceCategory: A certain set of related settings, Title parameters can be used to set the title, the same thing as a demarcation line, its top and bottom belong to different types of settings.

    2. CheckBox Preference: It's a CheckBox setting with only two values, true or false. Title parameters can be used to set the title, summaryOn and summaryOff parameters to set the prompt when the control is selected and unchecked, and defaultValue can be used to set the default value.

    3. ListPreference: The drop-down box selects the control, sets the title with Title parameter, sets the description with Summary parameter, clicks the drop-down box, sets the title of the drop-down box with dialog Title, and sets two arrays in res/values/array.xml to represent the contents and specific values in the drop-down box, entries and entryValues are respectively. Represents the displayed value and the real value obtained in the code. For example, array. XML is set as follows:

<?xml version ="1.0" encoding ="utf-8" ?>
<resources>
<string-array name ="entries_list_preference" >
    <item> test1</item>
    <item> test2</item>
    <item> test3</item>
</string-array>
<string-array name ="entriesvalue_list_preference" >
    <item> 1</item>
    <item> 2</item>
    <item> 3</item>
</string-array>
</resources>
  • 5.EditTextPreference: Input box control, you can enter string settings after clicking. Set the title with Title parameter, Summary parameter setting instructions, and dialogTitle parameter setting the title of the input box.
  • 6. Ringtone Preference: Ringtone Preference: Ringtone Selection Box, Click to select the system ringtone. Title parameter setting title, Summary parameter setting description, dialogTitle parameter setting ringtone selection box title.
  • 7. Customize preference layout:
  • 8. Clicking on a prefenrence requires a jump to open other activities:

    a. Adding intent subelements to preferences.xml

 <CheckBoxPreference
            android:defaultValue="true"
            android:key="checked_key"
            android:summaryOn="Show a summary for each link"
            android:title="Show Summaries">
            <intent
                android:action="android.intent.action.MAIN"
                android:targetPackage="com.example.wendy.preference"
                android:targetClass="com.example.wendy.preference.Main2Activity"/>
</CheckBoxPreference>
   b. Override onpreference TreeClick method in preference Acitivity
 @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        return false; //Click-jump can only be achieved by returning false, for the reasons referred to at http://blog.csdn.net/wujiangming/article/details/6218068
    }

This is the XML description of PreferenceActivity, so in the program we just need to create a new Activity inherited from PreferenceActivity, and then call it in the main program. The settings store in this PreferenceActivity is fully automated. You don't need to use code to store settings. After the PreferenceActivity is created, the system will automatically create a configuration file, / data/data/your_package_name/shared_prefs/(your_package_name)_preferences.xml. . The configuration files generated automatically are as follows:

<?xml version ='1.0' encoding ='utf-8' standalone ='yes' ?>
<map>
<string name ="EditTextPreference" >12332312</string>
<string name ="ListPreference" >2</string>
<string name ="RingtonePreference" >content://media/internal/audio/media/10</string>
<boolean name ="CheckBox1" value ="true" />
<boolean name ="CheckBox2" value ="true" />
</map>

The name attribute in the file corresponds to the key attribute in the xml file, which represents a set item. The value is unique, and the value in the middle of value and string represents the specific value of the set item.

Keywords: xml Android encoding Attribute

Added by aneuryzma on Mon, 08 Jul 2019 06:18:46 +0300