Android偏好设置(1)概述和Preferences简介
1.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.
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:
- 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 aPreferenceFragment
that displays your app settings. However, you can also usePreferenceActivity
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.
2.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 aString
.
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.
最新文章
- Oracle监听的静态注册和动态注册
- SPFA+寻路(行路难,洛谷2832)
- 多看Kindle的“导出失败,请检查网络或账号”错误的解决
- The constructor BASE64Encoder() is not accessible due to restriction on required
- linux下安装svn
- Token 的作用
- 登录MD5加盐处理
- BZOJ 1717 [Usaco2006 Dec]Milk Patterns 产奶的模式(后缀数组)
- WebStorm里启动electron项目
- linux系统下安装单台Redis
- linux_RAID
- 通过10046 event来获取真实的执行计划
- 语音识别(LSTM+CTC)
- koa1 源码详解1
- django中实现组合搜索
- Linux下启动Oracle服务和监听程序
- 384. Shuffle an Array(java,数组全排列,然后随机取)
- js实现右击
- TextView字体,行距,html格式,超链接,对大长度的设定
- python脚本监控获取当前Linux操作系统[内存]/[cpu]/[硬盘]/[登录用户]
热门文章
- libpython2.7.so.1.0: cannot open shared object file: No such file or directory
- centos 安装python2.7
- UTF-8 GBK UTF8 GB2312之间的区别和关系
- Solr 文章集成
- Eclipse:Some sites could not be found. See the error log for more detail.解决的方法
- 李洪强iOS开发之录音和播放实现
- Oracle学习(18)【DBA向】:分布式数据库
- pip 安装速度慢解决办法
- frameset使用总结
- jquery a