标签:
Instead of using View
objects to build the user interface, settings are built using various subclasses of thePreference
class that you declare in an XML file.
A Preference
object is the building block for a single setting. Each Preference
appears as an item in a list and provides the appropriate UI for users to modify the setting. For example, a CheckBoxPreference
creates a list item that shows a checkbox, and a ListPreference
creates an item that opens a dialog with a list of choices.
Each Preference
you add has a corresponding key-value pair that the system uses to save the setting in a default SharedPreferences
file for your app‘s settings. When the user changes a setting, the system updates the corresponding value in the SharedPreferences
file for you. The only time you should directly interact with the associated SharedPreferences
file is when you need to read the value in order to determine your app‘s behavior based on the user‘s setting.
The value saved in SharedPreferences
for each setting can be one of the following data types:
Set
Because your app‘s settings UI is built using Preference
objects instead of View
objects, you need to use a specialized Activity
or Fragment
subclass to display the list settings:
PreferenceActivity
class.Activity
that hosts a PreferenceFragment
that displays your app settings. However, you can also use PreferenceActivity
to create a two-pane layout for large screens when you have multiple groups of settings.How to set up your PreferenceActivity
and instances of PreferenceFragment
is discussed in the sections about Creating a Preference Activity and Using Preference Fragments.
Figure 1. Screenshots from the Android Messaging app‘s settings. Selecting an item defined by a Preference
opens an interface to change the setting.
Every setting for your app is represented by a specific subclass of the Preference
class. Each subclass includes a set of core properties that allow you to specify things such as a title for the setting and the default value. Each subclass also provides its own specialized properties and user interface. For instance, figure 1 shows a screenshot from the Messaging app‘s settings. Each list item in the settings screen is backed by a different Preference
object.
A few of the most common preferences are:
CheckBoxPreference
true
if it‘s checked).ListPreference
EditTextPreference
EditText
widget. The saved value is a String
.See the Preference
class for a list of all other subclasses and their corresponding properties.
Of course, the built-in classes don‘t accommodate every need and your application might require something more specialized. For example, the platform currently does not provide a Preference
class for picking a number or a date. So you might need to define your own Preference
subclass. For help doing so, see the section aboutBuilding a Custom Preference.
Android偏好设置(1)概述和Preferences简介
标签:
原文地址:http://www.cnblogs.com/cocl/p/4620966.html