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.
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 ...
随机推荐
- Codeforces div.2 B. The Child and Set
题目例如以下: B. The Child and Set time limit per test 1 second memory limit per test 256 megabytes input ...
- 在Linux环境下使用TCP的keepalive机制
Linux内置支持keepalive机制,为了使用它,你须要使能TCP/IP网络,为了可以配置内核在执行时的參数.你还须要procfs和sysctl的支持. 这个过程涉及到keepalive使用的三个 ...
- Python不同功能的函数
•函数作为参数 import math def add(x,y,f): return f(x) + f(y)print add(25,36,math.sqrt) •map()函数 map()是 Pyt ...
- IPython与Jupyter notebook 安装与配置,插件扩展,主题,PDF输出
基于 python2.7.13 32-bit版本安装 1.安装pyreadline https://pypi.python.org/pypi/pyreadline 下载对应的32位版本 安装Micro ...
- python 深浅复制与指针内存
Python是一门非常好的语言,他的长处在于拥有巨大灵活性的同一时候也拥有无比的严谨性,其它语言规定了非常多语法.告诉你什么情况下,语法就是这种,而Python却用非常少的规定,延伸出非常多语法,有些 ...
- 我的Android进阶之旅------>Android编译错误java.util.zip.ZipException: duplicate entry的解决方法
今天在Android Studio中把另外一个项目引入当前项目,编译的时候出现了java.util.zip.ZipException: duplicate entry错误. 错误例如以下所看到的: F ...
- mac下破解apk文件以及apktool的相关使用
Android apktool是一个用来处理APK文件的工具,可以对APK进行反编译生成程序的源代码和图片.XML配置.语言资源等文件,也可以添加新的功能到APK文件中.用该工具来汉化Android软 ...
- XMU 1606 nc与滴水问题 【模拟】
1606: nc与滴水问题 Time Limit: 1000 MS Memory Limit: 64 MBSubmit: 85 Solved: 27[Submit][Status][Web Boa ...
- 解决Linux环境Oracle显示乱码
首先查看当前的编码格式 select userenv('language') from dual; 解决方法: 一.临时解决方法 切换到Oracle用户,执行 export NLS_LAN ...
- bzoj2461: [BeiJing2011]符环
再做水题就没救了 考虑DP...我们把正反面一起弄 fi,j,k,u表示第几个位置,正面多了多少左括号,背面多了多少没办法消的右括号,背面结尾的左括号数 #include<cstdio> ...