Preferencescreen.docx
《Preferencescreen.docx》由会员分享,可在线阅读,更多相关《Preferencescreen.docx(50页珍藏版)》请在冰点文库上搜索。
Preferencescreen
只是实现一个perference,但是点击以后没有响应事件,我们可以定义一个一个Dialog,Dialog选项里面需要有图片,文字说明,后面还需要一个单选按钮,所以自己写了一个demo,效果图如下:
功能的完成是使用Dialog的addView()方法,把一个ListView添加进去。
ListView控件里面使用了ImageView和CheckedTextView控件,CheckedTextView是一个提供文字和选择框的控件。
如果对于CheckedTextView不熟悉,请自己查下文档,在这里就不在多说。
主要功能代码如下:
[java] viewplaincopy
1.public class ListViewActivityextends Activity {
2.
3. /** Called when the activity is first created. */
4.
5. @Override
6.
7. public void onCreate(Bundle savedInstanceState){
8.
9. super.onCreate(savedInstanceState);
10.
11. setContentView(R.layout.main);
12.
13. Button button=(Button)findViewById(R.id.button);
14.
15. //获取ListView
16.
17. final LayoutInflater factory = LayoutInflater.from(ListViewActivity.this);
18.
19. final View view = factory.inflate(
20.
21. R.layout.listview,null);
22.
23. final ListView list = (ListView) view.findViewById(R.id.ListView01);
24.
25. //把数据项添加到listItem里面
26.
27. ArrayList> listItem =newArrayList>();
28.
29. for(int i=0;i<5;i++)
30.
31. {
32.
33. if(i==0){
34.
35. HashMap map =new HashMap();
36.
37. map.put("ItemImage", R.drawable.checked);
38.
39. map.put("ItemTitle", "1");
40.
41. listItem.add(map);
42.
43. }else if(i==1){
44.
45. HashMap map =new HashMap();
46.
47. map.put("ItemImage", R.drawable.c);
48.
49. map.put("ItemTitle", "2");
50.
51. listItem.add(map);
52.
53. }else if(i==2){
54.
55. HashMap map =new HashMap();
56.
57. map.put("ItemImage", R.drawable.d);
58.
59. map.put("ItemTitle", "3");
60.
61. listItem.add(map);
62.
63. }else if(i==3){
64.
65. HashMap map =new HashMap();
66.
67. map.put("ItemImage", R.drawable.d);
68.
69. map.put("ItemTitle", "4");
70.
71. listItem.add(map);
72.
73. }else{
74.
75. HashMap map =new HashMap();
76.
77. map.put("ItemImage", R.drawable.e);
78.
79. map.put("ItemTitle", "5");
80.
81. listItem.add(map);
82.
83. }
84.
85. }
86.
87. //获得SimpleAdapter,并且把它添加到listView中
88.
89. SimpleAdapter listItemAdapter =new SimpleAdapter(this,listItem,
90.
91. R.layout.item,
92.
93. new String[] {"ItemImage","ItemTitle"},
94.
95. new int[] {R.id.imageView,R.id.checkedTextView}
96.
97. );
98.
99.
100.
101. list.setAdapter(listItemAdapter);
102.
103. list.setOnItemClickListener(new OnItemClickListener(){
104.
105.
106.
107. public void onItemClick(AdapterView
>arg0, View arg1,int arg2,
108.
109. long arg3) {
110.
111. //把所有的单选全部设为非选中
112.
113. for(int i=0;i114.
115. {
116. View v = list.getChildAt(i);
117.
118. CheckedTextViewcheckText=(CheckedTextView)v.findViewById(R.id.checkedTextView);
119.
120. checkText.setChecked(false);
121.
122. }
123. //获得点击项的CheckedTextView,并设为选中
124. CheckedTextViewcheck=(CheckedTextView)arg1.findViewById(R.id.checkedTextView);
125. check.setChecked(true);
126. }
127. });
128.
129. final AlertDialog.Builder builder=new AlertDialog.Builder(ListViewActivity.this);
130. button.setOnClickListener(new View.OnClickListener() {
131. public void onClick(View v) {
132. builder.setTitle("Dialog");
133. builder.setView(list);
134. builder.setNegativeButton("cencel",null);
135. builder.create().show();
136. }
137. });
138. }
139.}
其中item.xml代码如下
[html] viewplaincopy
1.
xml version="1.0"encoding="utf-8"?
>
2.
3.android="
4.
5. android:
orientation="horizontal"
6.
7. android:
layout_width="fill_parent"
8.
9. android:
layout_height="fill_parent"
10.
11. >
12.
13. 14.
15. android:
id="@+id/imageView"
16.
17. android:
layout_width="wrap_content"
18.
19. android:
layout_height="wrap_content"
20.
21. />
22.
23. android="
24.
25. android:
id="@+id/checkedTextView"
26.
27. android:
layout_width="match_parent"
28.
29. android:
layout_height="?
android:
attr/listPreferredItemHeight"
30.
31. android:
textAppearance="?
android:
attr/textAppearanceLarge"
32.
33. android:
gravity="center_vertical"
34.
35. android:
checkMark="?
android:
attr/listChoiceIndicatorSingle"
36.
37. android:
paddingLeft="6dip"
38.
39. android:
paddingRight="6dip"
40.
41./>
42.
43.
Listview.xml文件如下
[html] viewplaincopy
1.
xml version="1.0"encoding="utf-8"?
>
2.
3.android="
4.
5. android:
layout_width="wrap_content"
6.
7. android:
layout_height="wrap_content"
8.
9. android:
id="@+id/ListView01"
10.
11. />
PerferenceActivity是什么,看下面的截图:
Android系统截图
MusicPlayerSetting截图
好了,我们看到Android系统本身就大量用到了PreferenceActivity来对系统进行信息配置和管理,那么它是怎么保存数据的呢,如何创建PrefenceActivity的呢,更关键是怎样触发相应事件的呢。
先看第一个问题,PreferenceActivity是如何保存的数据。
对Android系统了解的都知道,Android系统有四种基本的数据保存方法,一是SharedPreference,二是文件,三是SQLite,四是ContentProvider。
看出来了吧,Preference,对就是使用SharedPreferneces以键值对的形式进行保存的,当然,我们也可以通过SharedPreferences来获取PreferenceActivity设置的值。
第二个问题:
如何创建一个PreferenceActivity。
其实Eclipse提供了相应的创建工具,和创建Layout是基本相同的。
步骤如下:
创建Android项目,并添加一个Androidxml文件。
注意,这次选择的不是Layout,而是Preference,而且注意Folder路径是res/xml.
添加完成之后,在res/xml/下打开添加的preference.xml文件。
可以看到Android也为我们提供两种编辑模式,可视化的结构设计及xml源码设计。
推荐使用structure进行创建。
如图所示:
下面我们看看PrefeneceActivity都提供了哪几种元素可供使用。
点击Add按钮,在打开的新窗口中可以看到以下几项:
CheckBoxPreference:
CheckBox选择项,对应的值的ture或flase。
如图:
EditTextPreference:
输入编辑框,值为String类型,会弹出对话框供输入。
ListPreference:
列表选择,弹出对话框供选择。
Preference:
只进行文本显示,需要与其他进行组合使用。
PreferenceCategory:
用于分组。
效果如下:
PreferenceScreen:
PreferenceActivity的根元素,必须为它。
RingtonePreference:
系统玲声选择。
OK,Preferenc的基本元素介绍完毕,下一节将使用它们创建一个完整的Preference并进行显示。
-------------------------------------------------------------------------------
分析MusicPlayerSetting,第一部分为“我的位置”,包括“使用无线网线”和“使用GPS”两个部分,而且都是CheckBox,根据上节学习,应该包括一个PreferenceCategory和两个CheckBoxPreference。
Xml代码:
title="我的位置"android:
key="set_local">
key="apply_wifi"
android:
summary="使用无线网络在应用程序(例如Google地图)中查看位置"android:
title="使用无线网络"android:
defaultValue="true">
key="apply_gps"
android:
summary="定位到街道级别(需要消耗更多的电量以及天气允许)"android:
title="使用GPS">
以上代码当然也可以用Android提供的IDE工具直接生成。
视频结构如下:
PreferenceCategory属性分析:
title:
显示的标题
key:
唯一标识(至少在同一程序中是唯一),SharedPreferences也将通过此Key值进行数据保存,也可以通过key值获取保存的信息(以下相同)。
CheckBoxPreference属性分析:
Key:
唯一标识.
title:
显示标题(大字体显示)
summary:
副标题(小字体显示)
defaultValue:
默认值(当然,此处只能是true或false了)
Preference.xml的第二部分为“无线和网络设置”,此部分包括的内容比较多,也稍微复杂,一步一步来分析。
xml代码:
title="无线和网络设置">
key="apply_fly"
android:
summary="禁用所有无线连接" android:
title="飞行模式">
key="apply_internet"
android:
summary="禁用通过USB共享Internet连接" android:
title="Internet共享">
key="apply_wifi"
android:
summary="打开Wi-Fi" android:
title="Wi-Fi">
summary="设置和管理无线接入点" android:
title="Wi-Fi设置"
android:
dependency="apply_wifi" android:
key="wifi_setting">
key="apply_bluetooth"
android:
summary="启用蓝牙" android:
title="蓝牙">
summary="管理连接、设备设备名称和可检测性"
android:
title="蓝牙设置" android:
dependency="apply_bluetooth"
android:
key="bluetooth_setting">
key="number_edit"
android:
title="输入电话号码">
title="部门设置" android:
entries="@array/department"
android:
entryValues="@array/department_value" android:
dialogTitle="选择部门"
android:
key="depart_value">
ringtoneType="all" android:
title="玲聲" android:
showDefault="true"android:
key="ring_key" android:
showSilent="true">
对应的St