Академический Документы
Профессиональный Документы
Культура Документы
and behaviors. For example, some apps allow users to specify whether
notifications are enabled or specify how often the application syncs data with
the cloud.
If you want to provide settings for your app, you should use Android's
Preference APIs to build an interface that's consistent with the user
experience in other Android apps (including the system settings).
Instead of using View objects to build the user interface, settings are built
using various subclasses of the Preference class that you declare in an
XML file.
Preferences
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 .
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:
Boolean
Float
Int
Long
String
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.
PreferenceScreen.
The root node for the XML file must be a <PreferenceScreen>
element. Within this element is where you add each Preference. Each
child you add within the <PreferenceScreen> element appears as a
single item in the list of settings.
android:title=Ringtones
android:key=ringtonePref />
<PreferenceScreen
android:title=Second Preference Screen
android:summary=Click here to go to the second Preference Screen
android:key=secondPrefScreenPref >
<EditTextPreference
android:summary=Enter a string
android:title=Edit Text (second Screen)
android:key=secondEditTextPref />
</PreferenceScreen>
</PreferenceCategory></PreferenceScreen>
android:key
This attribute is required for preferences that persist a data value. It
specifies the unique key (a string) the system uses when saving this
setting's value in the SharedPreferences.
The only instances in which this attribute is not required is when the
preference is a PreferenceCategory or PreferenceScreen, or
the preference specifies an Intent to invoke (with an <intent>
element) or a Fragment to display (with an android:fragment
attribute).
android:title
This provides a user-visible name for the setting.
android:defaultValue
This specifies the initial value that the system should set in the
SharedPreferences file. You should supply a default value for all
settings.