In the previous example, the Android ApiDemo sample resolution (9): App-> Activity-> Persistent State describes how to use Shared Preferences to store some statuses, shared Preferences is generally used to store some application Preferences (settings ). The android. preference package provides many classes for applications to conveniently display and Set Application-related preferences. Of course, you can use a custom UI to configure these program preferences. However, classes defined in android. preference can provide users with a unified UI (consistent with the Settings of Android ).
Generally, the program prefers to use a separate Activity (derived from PreferenceActivity. In PreferenceActivity, PreferenceScreen is the root element of Layout. It can contain other elements such as CheckBoxPreference, EditTextPreference, ListPreference, PreferenceCategory or RingtonPreference. when Preference is used, all program preferences are automatically stored in the SharedPreferences of the application. The application can access these preferences through getSharedPreferences.
• CheckBoxPreference uses Checkbox to display a configuration item.
• EditTextPreference uses a text box to display a certain brand item, allowing users to input text.
• ListPreference you can select a single button (list) from the list.
• MultiSelectListPreference uses a set of checkboxes, allowing this configuration item to have multiple values.
• RingtonPreference allows users to select a ringtone from
The base class of these classes is Preference. You can also define custom Preference based on the base class.
• PreferenceGroup can be used to define a group for multiple preferences. PreferenceCategory and PreferenceScreen are its subclasses.
• PreferenceCategory can also contain multiple Preferneces. If the group is Disable, you can provide a title.
• PreferenceScreen is the root element of the Preferences hierarchy. PreferenceScreen can be nested. The PreferenceScreen in the inner layer will use a new screen display, which is somewhat similar to the paging function in Word.
Preferences from XML describes how to use XML to define application Preferences and use PreferenceActivity to display Preferences.
The definition of R. xml. preferences is as follows:
<PreferenceScreen
Xmlns: android = "http://schemas.android.com/apk/res/android”>
<PreferenceCategory
Android: title = "@ string/inline_preferences">
<CheckBoxPreference
Android: key = "checkbox_preference"
Android: title = "@ string/title_toggle_preference"
Android: summary = "@ string/summary_toggle_preference"/>
</PreferenceCategory>
<PreferenceCategory
Android: title = "@ string/dialog_based_preferences">
<EditTextPreference
Android: key = "edittext_preference"
Android: title = "@ string/title_edittext_preference"
Android: summary = "@ string/summary_edittext_preference"
Android: dialogTitle = "@ string/dialog_title_edittext_preference"/>
<ListPreference
Android: key = "list_preference"
Android: title = "@ string/title_list_preference"
Android: summary = "@ string/summary_list_preference"
Android: entries = "@ array/entries_list_preference"
Android: entryValues = "@ array/entryvalues_list_preference"
Android: dialogTitle = "@ string/dialog_title_list_preference"/>
</PreferenceCategory>
<PreferenceCategory
Android: title = "@ string/launch_preferences">
<! -This PreferenceScreen tag serves as a screen break (similar to page break
In word processing). Like for other preference types, we assign a key
Here so it is able to save and restore its instance state.->
<PreferenceScreen
Android: key = "screen_preference"
Android: title = "@ string/title_screen_preference"
Android: summary = "@ string/summary_screen_preference">
<! -You can place more preferences here that will be shown on the next screen.->
<CheckBoxPreference
Android: key = "next_screen_checkbox_preference"
Android: title = "@ string/title_next_screen_toggle_preference"
Android: summary = "@ string/summary_next_screen_toggle_preference"/>
</PreferenceScreen>
<PreferenceScreen
Android: title = "@ string/title_intent_preference"
Android: summary = "@ string/summary_intent_preference">
<Intent android: action = "android. intent. action. VIEW"
Android: data = "http://www.android.com"/>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
Android: title = "@ string/preference_attributes">
<CheckBoxPreference
Android: key = "parent_checkbox_preference"
Android: title = "@ string/title_parent_preference"
Android: summary = "@ string/summary_parent_preference"/>
<! -The visual style of a child is defined by this styled theme attribute.->
<CheckBoxPreference
Android: key = "child_checkbox_preference"
Android: dependency = "parent_checkbox_preference"
Android: layout = "? Android: attr/preferenceLayoutChild"
Android: title = "@ string/title_child_preference"
Android: summary = "@ string/summary_child_preference"/>
</PreferenceCategory>
</PreferenceScreen>
In the code, addPreferencesFromResource (R. xml. preferences) is used to display the Preferences defined in XML.
The root element in XML is PreferenceScreen.
CheckBoxPreference
PreferenceCategory defines the configuration title of this group. CheckBoxPreference uses Checkbox to display this configuration item.
<PreferenceCategory
Android: title = "@ string/inline_preferences">
<CheckBoxPreference
Android: key = "checkbox_preference"
Android: title = "@ string/title_toggle_preference"
Android: summary = "@ string/summary_toggle_preference"/>
</PreferenceCategory>
EditTextPreference
EditTextPrefernece displays a text box to accept user input:
<EditTextPreference
Android: key = "edittext_preference"
Android: title = "@ string/title_edittext_preference"
Android: summary = "@ string/summary_edittext_preference"
Android: dialogTitle = "@ string/dialog_title_edittext_preference"/>
ListPreference
Displays a group of radio buttons.
<ListPreference
Android: key = "list_preference"
Android: title = "@ string/title_list_preference"
Android: summary = "@ string/summary_list_preference"
Android: entries = "@ array/entries_list_preference"
Android: entryValues = "@ array/entryvalues_list_preference"
Android: dialogTitle = "@ string/dialog_title_list_preference"/>
PreferenceScreen
Use the new screen to display the application preference configuration.
<PreferenceScreen
Android: key = "screen_preference"
Android: title = "@ string/title_screen_preference"
Android: summary = "@ string/summary_screen_preference">
<! -You can place more preferences here that will be shown on the next screen.->
<CheckBoxPreference
Android: key = "next_screen_checkbox_preference"
Android: title = "@ string/title_next_screen_toggle_preference"
Android: summary = "@ string/summary_next_screen_toggle_preference"/>
</PreferenceScreen>
In addition to a new screen, PreferenceScreen can also be used to start an Activity, which defines the startup browser to open the http://www.android.com.
<PreferenceScreen
Android: title = "@ string/title_intent_preference"
Android: summary = "@ string/summary_intent_preference">
<Intent android: action = "android. intent. action. VIEW"
Android: data = "http://www.android.com"/>
</PreferenceScreen>
The last example indicates that the dependency between Preference can be defined. The sub-Preferences is enabled only when the parent Preference is selected.
<PreferenceCategory
Android: title = "@ string/preference_attributes">
<CheckBoxPreference
Android: key = "parent_checkbox_preference"
Android: title = "@ string/title_parent_preference"
Android: summary = "@ string/summary_parent_preference"/>
<! -The visual style of a child is defined by this styled theme attribute.->
<CheckBoxPreference
Android: key = "child_checkbox_preference"
Android: dependency = "parent_checkbox_preference"
Android: layout = "? Android: attr/preferenceLayoutChild"
Android: title = "@ string/title_child_preference"
Android: summary = "@ string/summary_child_preference"/>
</PreferenceCategory>
Author: mapdigit