码迷,mamicode.com
首页 > 移动开发 > 详细

Android偏好设置(1)概述和Preferences简介

时间:2015-07-04 18:21:29      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

Overview


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.

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:

  • Boolean
  • Float
  • Int
  • Long
  • String
  • String 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:

  • If your app supports versions of Android older than 3.0 (API level 10 and lower), you must build the activity as an extension of the PreferenceActivity class.
  • On Android 3.0 and later, you should instead use a traditional Activity that hosts a PreferenceFragmentthat 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.

 

Preferences

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
Shows an item with a checkbox for a setting that is either enabled or disabled. The saved value is a boolean (true if it‘s checked).
ListPreference
Opens a dialog with a list of radio buttons. The saved value can be any one of the supported value types (listed above).
EditTextPreference
Opens a dialog with an 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!