PreferenceScreen application, preferencescreen
PreferenceScreen preference is preferred. PreferenceScreen translates PreferenceScreen into "preference display", which means you can flexibly define the display content style based on the characteristics, A screen can contain multiple styles, such as the built-in sound setting interface.
To achieve this display effect, you only need to use the PreferenceScreen class. Create an xml folder in the project resource file, and create a preferences. xml file in it.
If the root element is PreferenceScreen, the entire screen is displayed. The PreferenceCategory label is nested in the PreferenceCategory label, indicating the preference category. In the PreferenceCategory label, check boxes, input boxes, lists, and other display. the available controls are available on android. for more information about preference packages, see. after the xml file is compiled, it needs to be loaded into the activity. For xml loading of preference display, you can use addPreferencesFromResource () in PreferenceActivity. Therefore, the Activity must inherit PreferenceActivity. since the displayed screen contains the check box and the control in the input box, we must be interested in the selection or not and the content in the input box. How can we get the screen content?
The onPreferenceTreeClick method in the rewrite activity triggers this method when you perform operations on the content displayed on the screen.
Java code
- SharedPreferences contentPreference = preference. getSharedPreferences ();
- Boolean checkbox_toggle = contentPreference. getBoolean ("checkbox_preference", false );
- String animalName = contentPreference. getString ("edittext_preference", "default ");
You can get the input content on the screen, which is obtained in the form of key-value like SharedPreference object, where key is the value corresponding to the key attribute in the xml control label.
Display Effect:
Corresponding xml file:
Java code
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <PreferenceScreen
- Xmlns: android = "http://schemas.android.com/apk/res/android">
- <PreferenceCategory
- Android: title = "show preferences in a row">
- <CheckBoxPreference
- Android: key = "checkbox_preference"
- Android: title = "Switch Preferences"
- Android: summary = "this is a switch button"/>
- </PreferenceCategory>
- <PreferenceCategory
- Android: title = "dialog box-based preferences">
- <EditTextPreference
- Android: key = "edittext_preference"
- Android: title = "text input Preferences"
- Android: summary = "Use a text box dialog box"
- Android: dialogTitle = "Enter your pet"/>
- <ListPreference
- Android: key = "list_preference"
- Android: title = "list Preferences"
- Android: summary = "Use a List dialog box"
- Android: entries = "@ array/entries_list_preference"
- Android: entryValues = "@ array/entryvalues_list_preference"
- Android: dialogTitle = "select one"/>
- </PreferenceCategory>
- <PreferenceCategory
- Android: title = "Startup Preferences">
- <PreferenceScreen
- Android: key = "screen_preference"
- Android: title = "screen"
- Android: summary = "show another preference screen">
- <! -- You can place more preferences here that will be shown on the next screen. -->
- <CheckBoxPreference
- Android: key = "next_screen_checkbox_preference"
- Android: title = "Switch Preferences"
- Android: summary = "preferences on another screen"/>
- </PreferenceScreen>
- <PreferenceScreen
- Android: title = "intention Preferences"
- Android: summary = "starting an Activity with intent">
- <Intent android: action = "android. intent. action. VIEW"
- Android: data = "http://www.android.com"/>
- </PreferenceScreen>
- </PreferenceCategory>
- <PreferenceCategory
- Android: title = "Preference property">
- <CheckBoxPreference
- Android: key = "parent_checkbox_preference"
- Android: title = "parent switch"
- Android: summary = "this is a parent switch"/>
- <CheckBoxPreference
- Android: key = "child_checkbox_preference"
- Android: dependency = "parent_checkbox_preference"
- Android: layout = "? Android: attr/preferenceLayoutChild"
- Android: title = "sub-switch"
- Android: summary = "this is a sub-switch"/>
- </PreferenceCategory>
- </PreferenceScreen>