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 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.

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 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简介的更多相关文章

  1. Android偏好设置(2)为应用定义一个偏好设置xml

    1.Defining Preferences in XML Although you can instantiate new Preference objects at runtime, you sh ...

  2. Android偏好设置(6)应用和监听各偏好参数

    Reading Preferences By default, all your app's preferences are saved to a file that's accessible fro ...

  3. Android偏好设置(5)偏好设置界面显示多个分组,每个分组也有一个界面

    1.Using Preference Headers In rare cases, you might want to design your settings such that the first ...

  4. Android偏好设置(4)设置默认值

    Setting Default Values The preferences you create probably define some important behaviors for your ...

  5. Android偏好设置(3)启动偏好设置后显示的界面PreferenceActivity和PreferenceFragment

    Creating a Preference Activity To display your settings in an activity, extend the PreferenceActivit ...

  6. Android偏好设置(7)自定义Preference,和PreferenceDialog

    Building a Custom Preference The Android framework includes a variety of Preference subclasses that ...

  7. 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面

    我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...

  8. android之保存偏好设置信息到shareSharedPreferences,轻量级的保存数据的方法

    android之保存偏好设置信息到shareSharedPreferences,轻量级的保存数据的方法   SharedPreferences保存数据到xml文件 有时候要保存activity的某些状 ...

  9. Preferences偏好设置

    Preferences偏好设置-Preferences偏好设置 General(综合设置):在综合设置界面内,对Unity集成开发环境进行一些相关的设置 Auto Refresh:自动更新 Alway ...

随机推荐

  1. C/C++实现bmp文件读写

    之前知道点bmp图的格式,然后对8位操作过,然后今天弄了一下24位真彩色的. C++读取.旋转和保存bmp图像文件编程实现 主要是理解bmp文件的格式8/24位的区别 8位图有调色板,24位在文件头和 ...

  2. 判断所ping主机的操作系统

    根据它的值判断所ping主机的操作系统类型. TTL被称为生存期,也就是你所传输的数据在网络上经过的路由器的最大个数. 操作系统 TTLLINUX 64WIN2K/NT 128WINDOWS 系列 3 ...

  3. Android 项目导入常见错误

    1.SDK版本号不正确应,你能够打开你项目中的project.properties文件,改动target=android-18(我这是18) ,将18改 为14(其它都能够),再改回18会又一次载入. ...

  4. rand和srand的用法(转载)

    首先我们要对rand&srand有个总体的看法:srand初始化随机种子,rand产生随机数,下面将详细说明. rand(产生随机数)表头文件: #include<stdlib.h> ...

  5. HTML的简单学习

    <html>与</html>之间的部分用来描述网页. <body>与</body>之间是页面的可见的内容. <h1>与</h1> ...

  6. 【手记】小心在where中使用NEWID()的大坑 【手记】解决启动SQL Server Management Studio 17时报Cannot find one of more components...的问题 【C#】组件分享:FormDragger窗体拖拽器 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    [手记]小心在where中使用NEWID()的大坑 这个表达式: ABS(CHECKSUM(NEWID())) % 3 --把GUID弄成正整数,然后取模 是随机返回0.1.2这三个数,不可能返回其它 ...

  7. 为经典版eclipse添加web and JavaEE插件

    地址:http://download.eclipse.org/releases/juno. 选择Web,XML,Java EE and OSGI Enterprise Development,之后ne ...

  8. hduoj2094产生冠军

     产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  9. c++学习笔记之基础---类内声明线程函数的调用

    近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数exec_task中调用pthread_create去启动线程执行例程thread_rounter.编译之后报错如下: spf ...

  10. GEO,IGSO,MEO,LEO

    GEO(Geosynchronous Eearth Orbit):地球静止轨道卫星 IGSO(Inclined Geosynchronous Satellite Orbit):倾斜轨道同步卫星 地球同 ...