我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状态,Shared Preferences更一般的用法是用来存储一些应用程序偏好(设置)。

包android.preference 提供了很多类可以方便应用程序来显示和设置应用相关的偏好。当然你可以使用自定义的UI来配置这些程序偏好。但使用android.preference 中定义的类可以给用户一个统一的UI (和Android本身的Settings一致)。

通常情况下,程序偏好使用单独的Activity(派生于PreferenceActivity)来完成。在PreferenceActivity 中,PreferenceScreen为Layout 的root element ,它可以包含其它如:CheckBoxPreference ,EditTextPreference, ListPreference ,PreferenceCategory或RingtonPreference. 使用Preference时所有程序偏好将会自动保存在应用程序的SharedPreferences中, 应用可以通过getSharedPreferences() 来访问这些偏好设置。

  • CheckBoxPreference 使用Checkbox 来显示某个配置项。
  • EditTextPreference 使用文本框来显示某个配置项,允许接受用户输入文本。弹出dialog
  • ListPreference 使用一组单选钮 (列表)可以从中选择某一项。弹出dialog
  • MultiSelectListPreference 使用一组Checkbox,允许该配置项有多值。
  • RingtonPreference 允许用户从选取某个铃声

这些类的基类为Preference ,以它为基类,也可以定义自定义的Preference.

  • PreferenceGroup 可以为多个Preference定义一个组,PreferenceCategory, PreferenceScreen为它的子类。
  • PreferenceCategory 同样可以包含多个Preferneces ,如果该组被Disable时,可以提供一个标题。
  • PreferenceScreen 为 Preferences层次结构的根元素,PreferenceScreen可以实现嵌套。内层的PreferenceScreen将会使用一个新的屏幕显示,有点类似于Word中的分页功能。

Preferences from XML 介绍了使用XML来定义应用程序偏好,并使用PreferenceActivity来显示这个偏好。

R.xml.preferences的定义如下:

<!-- This is a primitive example showing the different types of preferences available. -->

<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory
android:title="@string/inline_preferences"> <CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_checkbox_preference"
android:summary="@string/summary_checkbox_preference" /> </PreferenceCategory> <PreferenceCategory
android:title="@string/dialog_based_preferences"> <EditTextPreference
android:key="edittext_preference"
android:title="@string/title_edittext_preference"
android:summary="@string/summary_edittext_preference"
android:dialogTitle="@string/dialog_title_edittext_preference"
/> <ListPreference
android:key="list_preference"
android:title="@string/title_list_preference"
android:summary="@string/summary_list_preference"
android:entries
="@array/entries_list_preference"
android:entryValues
="@array/entryvalues_list_preference"
android:dialogTitle="@string/dialog_title_list_preference" /> </PreferenceCategory> <PreferenceCategory
android:title="@string/launch_preferences"> <!-- This PreferenceScreen tag serves as a screen break (similar to page break
in word processing). Like for other preference types, we assign a key
here so it is able to save and restore its instance state. -->
<PreferenceScreen
android:key="screen_preference"
android:title="@string/title_screen_preference"
android:summary="@string/summary_screen_preference"> <!-- You can place more preferences here that will be shown on the next screen. --> <CheckBoxPreference
android:key="next_screen_checkbox_preference"
android:title="@string/title_next_screen_toggle_preference"
android:summary="@string/summary_next_screen_toggle_preference" /> </PreferenceScreen> <PreferenceScreen
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference"> <intent android:action="android.intent.action.VIEW"
android:data="http://www.android.com"
/> </PreferenceScreen> </PreferenceCategory> <PreferenceCategory
android:title="@string/preference_attributes"> <CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="@string/title_parent_preference"
android:summary="@string/summary_parent_preference" /> <!-- The visual style of a child is defined by this styled theme attribute. -->
<CheckBoxPreference
android:key="child_checkbox_preference"
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"

android:title="@string/title_child_preference"
android:summary="@string/summary_child_preference" /> </PreferenceCategory> </PreferenceScreen>

而在代码中使用addPreferencesFromResource(R.xml.preferences) 显示出XML所定义的Preferences。

XML中根元素为PreferenceScreen.

CheckBoxPreference

PreferenceCategory定义该组配置的标题,CheckBoxPreference使用Checkbox来显示该配置项。

    <PreferenceCategory
android:title="@string/inline_preferences"> <CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_checkbox_preference"
android:summary="@string/summary_checkbox_preference" /> </PreferenceCategory>

EditTextPreference

EditTextPrefernece显示一个文本框来接受用户输入:

        <EditTextPreference
android:key="edittext_preference"
android:title="@string/title_edittext_preference"
android:summary="@string/summary_edittext_preference"
android:dialogTitle="@string/dialog_title_edittext_preference" />

ListPreference

显示一组单选钮。

        <ListPreference
android:key="list_preference"
android:title="@string/title_list_preference"
android:summary="@string/summary_list_preference"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:dialogTitle="@string/dialog_title_list_preference" />

PreferenceScreen

使用新的屏幕显示该应用程序偏好配置。

        <!-- This PreferenceScreen tag serves as a screen break (similar to page break
in word processing). Like for other preference types, we assign a key
here so it is able to save and restore its instance state. -->
<PreferenceScreen
android:key="screen_preference"
android:title="@string/title_screen_preference"
android:summary="@string/summary_screen_preference"> <!-- You can place more preferences here that will be shown on the next screen. --> <CheckBoxPreference
android:key="next_screen_checkbox_preference"
android:title="@string/title_next_screen_toggle_preference"
android:summary="@string/summary_next_screen_toggle_preference" /> </PreferenceScreen>

Intent Preference

除了新起一个屏幕之外,PreferenceScreen也可以用来启动一个Activity,下面定义启动浏览器打开http://www.android.com。

        <PreferenceScreen
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference"> <intent android:action="android.intent.action.VIEW"
android:data="http://www.android.com" /> </PreferenceScreen>

Preference之间的依赖

最后一个例子表示可以定义Preference之间的依赖关系。子Preferences只有在父Preference选中时才被Enable。

    <PreferenceCategory
android:title="@string/preference_attributes"> <CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="@string/title_parent_preference"
android:summary="@string/summary_parent_preference" /> <!-- The visual style of a child is defined by this styled theme attribute. -->
<CheckBoxPreference
android:key="child_checkbox_preference"
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
android:title="@string/title_child_preference"
android:summary="@string/summary_child_preference" /> </PreferenceCategory>

【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面的更多相关文章

  1. 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01

    本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...

  2. 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程

    本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...

  3. 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener

    前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...

  4. 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式

    这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...

  5. 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState

    Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...

  6. 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考

    01 API Demos ApiDemos 详细介绍了Android平台主要的 API,android 5.0主要包括下图几个大类,涵盖了数百api示例:

  7. 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller

    Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,Local ...

  8. 【起航计划 034】2015 起航计划 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 绑定服务 ServiceConnection Binder

    本例和下列Local Service Controller 的Activity代码都定义在LocalServiceActivities.Java 中,作为LocalServiceActivities ...

  9. 【起航计划 033】2015 起航计划 Android APIDemo的魔鬼步伐 32 App->Service->Foreground Service Controller service使用,共享service,前台服务,onStartCommand

    Android系统也提供了一种称为“Service”的组件通常在后台运行.Activity 可以用来启动一个Service,Service启动后可以保持在后台一直运行,即使启动它的Activity退出 ...

随机推荐

  1. USACO 2.1.3 Sorting a Three-Valued Sequence(sort3)

    这道题就是给出由123三个值的一个数字序列,然后让你把这个序列升序排序,求最小的交换次数.注意这里可以不是相邻交换. 刚开始一看题的时候,还以为t=a a=b b=t那种水题呢,然后发现不是水题.. ...

  2. Property 'XXX' not found on type java.lang.String解决方案

    一,标签指令错误. 原指令标签: <%@ taglib prefix="c" uri="http://j ava.sun.com/jstl/core" % ...

  3. 记录Leetcode 鸡蛋掉落 的思路

    前言 首先看一下这个题目,是Leetcode的第887题"鸡蛋掉落": 你将获得 `K` 个鸡蛋,并可以使用一栋从 `1` 到 `N` 共有 `N` 层楼的建筑. 每个蛋的功能都是 ...

  4. 本地访问Vmware虚机Web网站

    情况:公司是域环境,Vmware网络设置的是NAT连接模式,里外装的都是Windows,虚机网络IP地址是自动获取的. 查看: 1.虚机Ping本地的IP地址可以Ping通: 2.本地Ping虚机的I ...

  5. 免费的mysql数据库

    https://blog.csdn.net/kernel_/article/details/53320498

  6. spring aop execution用法

    代码结构: 1. "execution(* com.ebc..*.*(..))" 与 "execution(*  com.ebc..*(..))" 2019-0 ...

  7. Linux文件扩展名

    在linux中,扩展名没什么实际意义.但是为了兼容winodows,同时便于我们大多数习惯了windows的用户区分文件的不同,所以我们还是习惯通过扩展名来表示不同的文件类型. ①系统文件 扩展名 说 ...

  8. java——斗地主小游戏之洗牌发牌

    遇到的问题: 1.int和Integer的区别? 1)Integer是int的包装类,int则是java的一种基本数据类型 . 2)Integer变量必须实例化后才能使用,而int变量不需要 . 3) ...

  9. shell中各种括号()、(())、[]、[[]]、{}的作用和区别

    查看原文 - CSDN

  10. RTT学习之BSP

    ---恢复内容开始--- 一 根据相近型号的demo BSP进行修改制作自己的BSP https://github.com/RT-Thread/rt-thread/blob/master/bsp/st ...