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
PreferenceActivityclass. - On Android 3.0 and later, you should instead use a traditional
Activitythat hosts aPreferenceFragmentthat displays your app settings. However, you can also usePreferenceActivityto 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 (
trueif 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
EditTextwidget. 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.
Android偏好设置(1)概述和Preferences简介的更多相关文章
- Android偏好设置(2)为应用定义一个偏好设置xml
1.Defining Preferences in XML Although you can instantiate new Preference objects at runtime, you sh ...
- Android偏好设置(6)应用和监听各偏好参数
Reading Preferences By default, all your app's preferences are saved to a file that's accessible fro ...
- Android偏好设置(5)偏好设置界面显示多个分组,每个分组也有一个界面
1.Using Preference Headers In rare cases, you might want to design your settings such that the first ...
- Android偏好设置(4)设置默认值
Setting Default Values The preferences you create probably define some important behaviors for your ...
- Android偏好设置(3)启动偏好设置后显示的界面PreferenceActivity和PreferenceFragment
Creating a Preference Activity To display your settings in an activity, extend the PreferenceActivit ...
- Android偏好设置(7)自定义Preference,和PreferenceDialog
Building a Custom Preference The Android framework includes a variety of Preference subclasses that ...
- 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面
我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...
- android之保存偏好设置信息到shareSharedPreferences,轻量级的保存数据的方法
android之保存偏好设置信息到shareSharedPreferences,轻量级的保存数据的方法 SharedPreferences保存数据到xml文件 有时候要保存activity的某些状 ...
- Preferences偏好设置
Preferences偏好设置-Preferences偏好设置 General(综合设置):在综合设置界面内,对Unity集成开发环境进行一些相关的设置 Auto Refresh:自动更新 Alway ...
随机推荐
- I2C上拉电阻取值范围
I2C总线是微电子通信控制领域中常用的一种总线标准,具备接线少,控制简单,速率高等优点.在I2C电路中常见的上拉电阻有1k.1.5k.2.2k.4.7k.5.1k.10k等等,但是应该如何根据开发要求 ...
- STM32通过调用库函数进行编程
1.调用库函数编程和直接配置寄存器编程的差别: 2.CMSIS标准: 3.STM32库函数的组织: 4.程序例举: 调用库函数实现通过USART发送数据(26个大写的英文字母) 首先:在主函数部分先要 ...
- 常见网络摄像机默认使用的端口,RTSP地址
品牌 默认IP地址 WEB RTSP HTTPS 数据 ONVIF 海康威视 192.168.1.64/DHCP用户名admin 密码自己设 80 554 443 8000 80 大华 192 ...
- hadoop打jar包
编译: javac -classpath hadoop的路径下面/hadoop-0.20.0-core.jar -d .class文件存放的路径 XXXX.j ...
- How can I pass data from Flask to JavaScript in a template?
https://stackoverflow.com/questions/11178426/how-can-i-pass-data-from-flask-to-javascript-in-a-templ ...
- JDBC各种数据库连接URL关键代码
通过JDBC连接数据库时,各个数据库有着不同的URL格式,为了方便大家使用,我在以下提供了常见的7种数据库连接示例代码,请根据实际需要进行相应的更改. 1.Oracle数据库 Class.forNam ...
- assert的作用是什么
assert()宏是用于保证满足某个特定条件,用法是: assert(表达式); 如果表达式的值为假,整个程序将退出,并输出一条错误信息.如果表达式的值为真则继续执行后面的语句. 使用这个宏前需要包含 ...
- get the page name from url
https://stackoverflow.com/questions/1874532/better-way-to-get-page-name The way I interpret the ques ...
- Android沉浸式状态栏(透明状态栏)最佳实现
Android沉浸式状态栏(透明状态栏)最佳实现 在Android4.4之前,我们的应用没法改变手机的状态栏颜色,当我们打开应用时,会出现上图中左侧的画面,在屏幕的顶部有一条黑色的状态栏,和应用的风格 ...
- 小程序-demo:小程序示例-page/component
ylbtech-小程序-demo:小程序示例-page/component 以下将展示小程序官方组件能力,组件样式仅供参考,开发者可根据自身需求自定义组件样式,具体属性参数详见小程序开发文档. 1. ...