【起航计划 030】2015 起航计划 Android APIDemo的魔鬼步伐 29 App->Preferences->Preferences from code
这里我们使用类比的方法,将 PreferenceActivity 与一般的Activity 作个类比,可以更好的理解Android.Preference中的各个类。
| PreferenceActivity | Activity |
| Preference | View |
| PreferenceGroup | ViewGroup |
| PreferenceScreen | LinearLayout |
| CheckBoxPreference | CheckBox |
| EditTextPreference | EditText |
| ListPreference | List |
| PreferenceCategory | ViewGroup |
| … | .. |
PreferenceActivity 的UI是通过Preference等来定义,和Activity的ContentView一样,PreferenceActivity 的UI可以通过XML定义,也可以使用代码来定义,本例就是使用代码来定义PreferenceActivity 的UI。部分代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
setPreferenceScreen(root);
populatePreferenceHierarchy(root);
} private void populatePreferenceHierarchy(PreferenceScreen root) {
// Inline preferences
PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
inlinePrefCat.setTitle(R.string.inline_preferences);
root.addPreference(inlinePrefCat); // Checkbox preference
CheckBoxPreference checkboxPref = new CheckBoxPreference(this);
checkboxPref.setKey("checkbox_preference");
checkboxPref.setTitle(R.string.title_checkbox_preference);
checkboxPref.setSummary(R.string.summary_checkbox_preference);
inlinePrefCat.addPreference(checkboxPref); // Switch preference
SwitchPreference switchPref = new SwitchPreference(this);
switchPref.setKey("switch_preference");
switchPref.setTitle(R.string.title_switch_preference);
switchPref.setSummary(R.string.summary_switch_preference);
inlinePrefCat.addPreference(switchPref); // Dialog based preferences
PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences);
root.addPreference(dialogBasedPrefCat); // Edit text preference
EditTextPreference editTextPref = new EditTextPreference(this);
editTextPref.setDialogTitle(R.string.dialog_title_edittext_preference);
editTextPref.setKey("edittext_preference");
editTextPref.setTitle(R.string.title_edittext_preference);
editTextPref.setSummary(R.string.summary_edittext_preference);
dialogBasedPrefCat.addPreference(editTextPref); // List preference
ListPreference listPref = new ListPreference(this);
listPref.setEntries(R.array.entries_list_preference);
listPref.setEntryValues(R.array.entryvalues_list_preference);
listPref.setDialogTitle(R.string.dialog_title_list_preference);
listPref.setKey("list_preference");
listPref.setTitle(R.string.title_list_preference);
listPref.setSummary(R.string.summary_list_preference);
dialogBasedPrefCat.addPreference(listPref); // Launch preferences
PreferenceCategory launchPrefCat = new PreferenceCategory(this);
launchPrefCat.setTitle(R.string.launch_preferences);
root.addPreference(launchPrefCat); /*
* The Preferences screenPref serves as a screen break (similar to page
* break in word processing). Like for other preference types, we assign
* a key here so that it is able to save and restore its instance state.
*/
// Screen preference
PreferenceScreen screenPref = getPreferenceManager().createPreferenceScreen(this);
screenPref.setKey("screen_preference");
screenPref.setTitle(R.string.title_screen_preference);
screenPref.setSummary(R.string.summary_screen_preference);
launchPrefCat.addPreference(screenPref); /*
* You can add more preferences to screenPref that will be shown on the
* next screen.childCheckBoxPref.setDependency(PARENT_CHECKBOX_PREFERENCE);
*/ // Example of next screen toggle preference
CheckBoxPreference nextScreenCheckBoxPref = new CheckBoxPreference(this);
nextScreenCheckBoxPref.setKey("next_screen_toggle_preference");
nextScreenCheckBoxPref.setTitle(R.string.title_next_screen_toggle_preference);
nextScreenCheckBoxPref.setSummary(R.string.summary_next_screen_toggle_preference);
screenPref.addPreference(nextScreenCheckBoxPref); // Intent preference
PreferenceScreen intentPref = getPreferenceManager().createPreferenceScreen(this);
intentPref.setIntent(new Intent().setAction(Intent.ACTION_VIEW)
.setData(Uri.parse("http://www.android.com")));
intentPref.setTitle(R.string.title_intent_preference);
intentPref.setSummary(R.string.summary_intent_preference);
launchPrefCat.addPreference(intentPref); // Preference attributes
PreferenceCategory prefAttrsCat = new PreferenceCategory(this);
prefAttrsCat.setTitle(R.string.preference_attributes);
root.addPreference(prefAttrsCat); // Visual parent toggle preference
CheckBoxPreference parentCheckBoxPref = new CheckBoxPreference(this);
parentCheckBoxPref.setTitle(R.string.title_parent_preference);
parentCheckBoxPref.setSummary(R.string.summary_parent_preference);
prefAttrsCat.addPreference(parentCheckBoxPref);
parentCheckBoxPref.setKey(PARENT_CHECKBOX_PREFERENCE); // Visual child toggle preference
// See res/values/attrs.xml for the <declare-styleable> that defines
// TogglePrefAttrs.
TypedArray a = obtainStyledAttributes(R.styleable.TogglePrefAttrs);
CheckBoxPreference childCheckBoxPref = new CheckBoxPreference(this);
childCheckBoxPref.setTitle(R.string.title_child_preference);
childCheckBoxPref.setSummary(R.string.summary_child_preference);
childCheckBoxPref.setLayoutResource(
a.getResourceId(R.styleable.TogglePrefAttrs_android_preferenceLayoutChild,
0));
prefAttrsCat.addPreference(childCheckBoxPref);
childCheckBoxPref.setDependency(PARENT_CHECKBOX_PREFERENCE);
a.recycle();
}
基本上和使用XML定义一一对应。

【起航计划 030】2015 起航计划 Android APIDemo的魔鬼步伐 29 App->Preferences->Preferences from code的更多相关文章
- 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01
本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...
- 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程
本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener
前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...
- 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面
我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...
- 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式
这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState
Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...
- 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考
01 API Demos ApiDemos 详细介绍了Android平台主要的 API,android 5.0主要包括下图几个大类,涵盖了数百api示例:
- 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller
Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,Local ...
- 【起航计划 034】2015 起航计划 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 绑定服务 ServiceConnection Binder
本例和下列Local Service Controller 的Activity代码都定义在LocalServiceActivities.Java 中,作为LocalServiceActivities ...
随机推荐
- luogu4088 [USACO18FEB]Slingshot
link 这题在线得写树套树,所以我写的离线+树状数组 对于每个询问,Ans=\(\max_{j=1}^n{|a_j-x_i|+|b_j-y_i|+t_i}\) 拆成四种情况 \(x_i\le a_j ...
- Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)
题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...
- 13.Convert BST to Greater Tree(将树转为更大树)
Level: Easy 题目描述: Given a Binary Search Tree (BST), convert it to a Greater Tree such that every k ...
- Java实现微信小程序支付(准备)
Java语言开发微信小程序支付功能: 1.通过https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1路径到官方下载Java的支付SD ...
- react 拆分组件于组件
Todolist.js(这是父组件) import React, { Component,Fragment } from 'react'; import './style.css'; import T ...
- Mybatis学习笔记(七) —— 关联查询
一.一对多查询 需求:查询所有订单信息,关联查询下单用户信息. 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则 ...
- 奇妙的 clip-path 几何图形
CSS 新属性 clip-path,意味裁剪路径的意思,让我们可以很便捷的生成各种几何图形. clip-path 通过定义特殊的路径,实现我们想要的图形.而这个路径,正是 SVG 中的 path . ...
- Codeforces Round #532 (Div. 2)- B(思维)
Arkady coordinates rounds on some not really famous competitive programming platform. Each round fea ...
- Jenkins自动化CI CD流水线之6--构建邮件状态通知
一. 前提 前提: 服务器开启邮箱服务: 二. 基础配置 需要安装一个插件: 插件: Email Extension Plugin 进行配置: 系统管理->系统设置-> 相关配置如下图: ...
- Hive 报错信息及解决方法
return code 2 为SQL报错. return code 1 一般为权限问题. 具体要看源码.