Android软件开发之PreferenceActivity中的组件

Android软件开发之PreferenceActivity中的组件




雨松MOMO原创文章如转载,请注明:转载至我的独立域名博客雨松MOMO程序研究院,原文地址:http://www.xuanyusong.com/archives/155




Android软件开发之PreferenceActivity中的组件


1.PreferenceActivity 介绍

PreferenceActivity 继承ListActivity 它是以一个列表的形式在展现内容,它最主要的特点是添加Preference可以让控件的状态持久化储存,举个例子 比如用户选中checkbox后 退出应用然后在进入应用,这时用户希望看到的是checkbox被选中,所以软件须要记录用户每次操作的过程并且持久储存,在进入应用的时候须要判断这些久储存的数据然后将系统控件的状态呈现在UI中。
尤其是软件开发肯定会有一堆设置选项选项,每次进入Activity都去手动的去取储存的数据,这样代码会变得很复杂很麻烦。 这个时候Preference就出来了,它就是专门解决这些特殊的选项保存与读取的显示。用户每次操作事件它会及时的以键值对的形式记录在SharedPreferences中,Activity每次启动它会自动帮我们完成数据的读取以及UI的显示。
android开发中一共为我们提供了4个组件,分别是CheckBoxPreference组件、EditTextPreference组件、ListPreference组件、RingtonePreference组件,下面我用一个例子一一向同学们介绍一下。




2.CheckBoxPreference组件

CheckBoxPreference 选中为true 取消选中为false 它的值会以boolean的形式储存在SharedPreferences中。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <PreferenceCategoryandroid:title="CheckBoxPreference">
  5. <CheckBoxPreferenceandroid:key="checkbox_0"
  6. android:title="CheckBox_A"
  7. android:summary="这是一个勾选框A">
  8. </CheckBoxPreference>
  9. <CheckBoxPreferenceandroid:key="checkbox_1"
  10. android:title="CheckBox_B"
  11. android:summary="这是一个勾选框B">
  12. </CheckBoxPreference>
  13. </PreferenceCategory>
  14. </PreferenceScreen>



Android软件开发之PreferenceActivity中的组件

  1. importandroid.content.Context;
  2. importandroid.os.Bundle;
  3. importandroid.preference.CheckBoxPreference;
  4. importandroid.preference.Preference;
  5. importandroid.preference.PreferenceActivity;
  6. importandroid.preference.Preference.OnPreferenceChangeListener;
  7. importandroid.preference.Preference.OnPreferenceClickListener;
  8. importandroid.widget.Toast;
  9. publicclassCheckBoxActivityextendsPreferenceActivity{
  10. ContextmContext=null;
  11. @Override
  12. protectedvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. //从资源文件中添Preferences,选择的值将会自动保存到SharePreferences
  15. addPreferencesFromResource(R.xml.checkbox);
  16. mContext=this;
  17. //CheckBoxPreference组件
  18. CheckBoxPreferencemCheckbox0=(CheckBoxPreference)findPreference("checkbox_0");
  19. mCheckbox0.setOnPreferenceClickListener(newOnPreferenceClickListener(){
  20. @Override
  21. publicbooleanonPreferenceClick(Preferencepreference){
  22. //这里可以监听到这个CheckBox的点击事件
  23. returntrue;
  24. }
  25. });
  26. mCheckbox0.setOnPreferenceChangeListener(newOnPreferenceChangeListener(){
  27. @Override
  28. publicbooleanonPreferenceChange(Preferencearg0,ObjectnewValue){
  29. //这里可以监听到checkBox中值是否改变了
  30. //并且可以拿到新改变的值
  31. Toast.makeText(mContext,"checkBox_0改变的值为"+(Boolean)newValue,Toast.LENGTH_LONG).show();
  32. returntrue;
  33. }
  34. });
  35. CheckBoxPreferencemCheckbox1=(CheckBoxPreference)findPreference("checkbox_1");
  36. mCheckbox1.setOnPreferenceClickListener(newOnPreferenceClickListener(){
  37. @Override
  38. publicbooleanonPreferenceClick(Preferencepreference){
  39. //这里可以监听到这个CheckBox的点击事件
  40. returntrue;
  41. }
  42. });
  43. mCheckbox1.setOnPreferenceChangeListener(newOnPreferenceChangeListener(){
  44. @Override
  45. publicbooleanonPreferenceChange(Preferencearg0,ObjectnewValue){
  46. //这里可以监听到checkBox中值是否改变了
  47. //并且可以拿到新改变的值
  48. Toast.makeText(mContext,"checkBox_1改变的值为"+(Boolean)newValue,Toast.LENGTH_LONG).show();
  49. returntrue;
  50. }
  51. });
  52. }
  53. }

3.EditTextPreference组件

EditTextPreference 点击后会弹出一个输入框,输入的内容会以字符串的的形式储存在SharedPreferences中。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <PreferenceCategoryandroid:title="EditTextPreference">
  5. <EditTextPreferenceandroid:key="edit_0"
  6. android:title="输入信息_A"
  7. android:summary="请输入您的信息"
  8. android:defaultValue="请输入信息"
  9. android:dialogTitle="输入框">
  10. </EditTextPreference>
  11. <EditTextPreferenceandroid:key="edit_1"
  12. android:title="输入信息_B"
  13. android:summary="请输入您的信息"
  14. android:defaultValue="请输入信息"
  15. android:dialogTitle="输入框">
  16. </EditTextPreference>
  17. </PreferenceCategory>
  18. </PreferenceScreen>

Android软件开发之PreferenceActivity中的组件Android软件开发之PreferenceActivity中的组件

  1. importandroid.content.Context;
  2. importandroid.os.Bundle;
  3. importandroid.preference.EditTextPreference;
  4. importandroid.preference.PreferenceActivity;
  5. publicclassEditTextActivityextendsPreferenceActivity{
  6. ContextmContext=null;
  7. @Override
  8. protectedvoidonCreate(BundlesavedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. //从资源文件中添Preferences,选择的值将会自动保存到SharePreferences
  11. addPreferencesFromResource(R.xml.edittext);
  12. mContext=this;
  13. //EditTextPreference组件
  14. EditTextPreferencemEditText=(EditTextPreference)findPreference("edit_0");
  15. //设置dialog按钮信息
  16. mEditText.setPositiveButtonText("确定");
  17. mEditText.setNegativeButtonText("取消");
  18. //设置按钮图标
  19. mEditText.setDialogIcon(R.drawable.jay);
  20. }
  21. }

4.ListPreference组件

在res/array中先写两个数组,一个用与list的显示内容,一个用户list的选中数值。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <string-arrayname="auto_logout_time_key">
  4. <item>10mins.</item>
  5. <item>20mins.</item>
  6. <item>30mins.</item>
  7. <item>60mins.</item>
  8. </string-array>
  9. <string-arrayname="auto_logout_time_value">
  10. <item>600000</item>
  11. <item>1200000</item>
  12. <item>1800000</item>
  13. <item>3600000</item>
  14. </string-array>
  15. </resources>

ListPreference点击后会弹出一个列表框,选中后会将选中的内容(上面数组中的值)会以字符串的的形式储存在SharedPreferences中。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <PreferenceCategoryandroid:title="ListPreference">
  5. <ListPreference
  6. android:key="list_0"
  7. android:title="登录设置A"
  8. android:dialogTitle="选择在线时间"
  9. android:entries="@array/auto_logout_time_key"
  10. android:entryValues="@array/auto_logout_time_value">
  11. </ListPreference>
  12. <ListPreference
  13. android:key="list_0"
  14. android:title="登录设置A"
  15. android:dialogTitle="选择在线时间"
  16. android:entries="@array/auto_logout_time_key"
  17. android:entryValues="@array/auto_logout_time_value">
  18. </ListPreference>
  19. </PreferenceCategory>
  20. </PreferenceScreen>

Android软件开发之PreferenceActivity中的组件

  1. importandroid.os.Bundle;
  2. importandroid.preference.PreferenceActivity;
  3. publicclassListActivityextendsPreferenceActivity{
  4. @Override
  5. protectedvoidonCreate(BundlesavedInstanceState){
  6. super.onCreate(savedInstanceState);
  7. //从资源文件中添Preferences,选择的值将会自动保存到SharePreferences
  8. addPreferencesFromResource(R.xml.list);
  9. }
  10. }

5.RingtonePreference组件

RingtonePreference点击后会弹出一个系统铃声的列表框,选中后会将选中的内容(uri字符集)会以字符串的的形式储存在SharedPreferences中。


  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <PreferenceCategoryandroid:title="RingtonePreference">
  5. <RingtonePreference
  6. android:key="ringtone_0"
  7. android:summary="选择系统铃声A"
  8. android:title="铃声设置"
  9. android:ringtoneType="all"
  10. android:showSilent="true"></RingtonePreference>
  11. <RingtonePreference
  12. android:key="ringtone_!"
  13. android:summary="选择系统铃声B"
  14. android:title="铃声设置"
  15. android:ringtoneType="all"
  16. android:showSilent="true"></RingtonePreference>
  17. </PreferenceCategory>
  18. </PreferenceScreen>

android:ringtoneType 系统一共提供了4中响铃模式的类型分别为 铃声(ringtone) 通知( notification) 警告(alarm) 全部(all)

模拟器默认是没有铃声的,下图中的铃声我是将歌曲文件拷贝到SD卡中,设置铃声后才会出现的。如果觉得拷贝麻烦可以使用豌豆荚或者91助手将歌曲文件放入手机SD卡中,在铃声设置那里设置一下在这里就会出现。





Android软件开发之PreferenceActivity中的组件

  1. importandroid.os.Bundle;
  2. importandroid.preference.PreferenceActivity;
  3. publicclassRingtoneActivityextendsPreferenceActivity{
  4. @Override
  5. protectedvoidonCreate(BundlesavedInstanceState){
  6. super.onCreate(savedInstanceState);
  7. //从资源文件中添Preferences,选择的值将会自动保存到SharePreferences
  8. addPreferencesFromResource(R.xml.ringtone);
  9. }
  10. }


5.自定义控件

使用系统的控件在显示方面难免会有些单一,如果想做一个好看的界面就需要使用自定义Preference。下面我简单说明一下如何编写自定义Preference。首先在res/layout中添加preferences文件

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#00000000">
  6. <LinearLayout
  7. android:gravity="center_vertical"
  8. android:background="@drawable/preference_mid_background"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. >
  12. <ImageView
  13. android:focusable="false"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"android:src="@drawable/setting_about_us">
  16. </ImageView>
  17. <RelativeLayout
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_marginLeft="15dip"
  21. android:layout_marginTop="6dip"
  22. android:layout_marginRight="6dip"
  23. android:layout_marginBottom="6dip"
  24. android:layout_weight="1"
  25. >
  26. <TextView
  27. android:textSize="15dip"
  28. android:textColor="#000000"
  29. android:ellipsize="marquee"
  30. android:id="@+android:id/title"
  31. android:fadingEdge="horizontal"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:singleLine="true"
  35. >
  36. </TextView>
  37. <TextView
  38. android:textAppearance="?android:attr/textAppearanceSmall"
  39. android:textColor="#565656"
  40. android:id="@+android:id/summary"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:maxLines="4"
  44. android:layout_below="@+android:id/title"
  45. android:layout_alignLeft="@+android:id/title"
  46. >
  47. </TextView>
  48. </RelativeLayout>
  49. <ImageView
  50. android:focusable="false"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:background="@drawable/preference_arrows"/>
  54. </LinearLayout>
  55. </LinearLayout>

android:background="@drawable/preference_mid_background"
通过这一行可以设置这个按钮的点击、选中默认的显示状态,这样可以让你的按钮更加好看。须要在res/drawable中添加xml文件

android:state_facused :为控件选中显示
android:state_pressed:为控件按下显示
最后一个为默认显示



  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <selector
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <item
  5. android:state_focused="true"
  6. android:drawable="@drawable/preference_mid_pressed"
  7. >
  8. </item>
  9. <item
  10. android:state_pressed="true"
  11. android:drawable="@drawable/preference_mid_pressed"
  12. >
  13. </item>
  14. <item
  15. android:drawable="@drawable/preference_mid"
  16. >
  17. </item>
  18. </selector>


Android软件开发之PreferenceActivity中的组件

  1. importandroid.content.Context;
  2. importandroid.os.Bundle;
  3. importandroid.preference.Preference;
  4. importandroid.preference.PreferenceActivity;
  5. importandroid.preference.Preference.OnPreferenceClickListener;
  6. importandroid.widget.Toast;
  7. publicclassAllActivityextendsPreferenceActivity{
  8. /**自定义布局A**/
  9. Preferencepreference0=null;
  10. /**自定义布局B**/
  11. Preferencepreference1=null;
  12. ContextmContext=null;
  13. @Override
  14. protectedvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. //从资源文件中添Preferences,选择的值将会自动保存到SharePreferences
  17. addPreferencesFromResource(R.xml.all);
  18. mContext=this;
  19. preference0=findPreference("pref_key_0");
  20. preference0.setOnPreferenceClickListener(newOnPreferenceClickListener(){
  21. @Override
  22. publicbooleanonPreferenceClick(Preferencepreference){
  23. Toast.makeText(mContext,"自定义布局A被按下",Toast.LENGTH_LONG).show();
  24. returnfalse;
  25. }
  26. });
  27. preference1=findPreference("pref_key_1");
  28. preference1.setOnPreferenceClickListener(newOnPreferenceClickListener(){
  29. @Override
  30. publicbooleanonPreferenceClick(Preferencepreference){
  31. Toast.makeText(mContext,"自定义布局B被按下",Toast.LENGTH_LONG).show();
  32. returnfalse;
  33. }
  34. });
  35. }
  36. }

读取数据

在PreferenceActivity中可以用下面这种方式拿到SharedPreferences中储存的数值,通过PreferenceManager.getDefaultSharedPreferences(this) 方法拿到控件默认储存的sharedPreferences对象。


  1. SharedPreferencesprefs=PreferenceManager.getDefaultSharedPreferences(this);
  2. booleansomething=prefs.getBoolean("something",false);




在模拟起中将SharedPreferences储存内容拷贝出来后,可以清楚的看到通过点击系统控件储存的数值。这里我说一下铃声的储存,它是以一个字符串形式的uri字符集,它所指向的是系统铃声储存的路径。所以根据这个字符集就可以找到这个铃声。


  1. <?xmlversion='1.0'encoding='utf-8'standalone='yes'?>
  2. <map>
  3. <stringname="ringtone_!">content://media/external/audio/media/1</string>
  4. <stringname="ringtone_0">content://media/external/audio/media/1</string>
  5. <stringname="list_0">1800000</string>
  6. <stringname="edit_1">请输入信息1212</string>
  7. <stringname="list">1200000</string>
  8. <stringname="ringtone">content://settings/system/ringtone</string>
  9. <booleanname="checkbox_0"value="true"/>
  10. <booleanname="checkbox_1"value="true"/>
  11. <stringname="edit_0">请输入信息</string>
  12. </map>





今天出去散了散心,心情挺开心的,哇咔咔~~总的来说这一篇在实际的软件开发中还是非常重要的,老规矩每篇文章都会附带源代码,最后如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习雨松MOMO希望可以和大家一起进步。


下载地址:http://download.****.net/source/3556196
上一篇:Android游戏开发之横竖屏的切换(二十七)
Android软件开发之PreferenceActivity中的组件




雨松MOMO原创文章如转载,请注明:转载至我的独立域名博客雨松MOMO程序研究院,原文地址:http://www.xuanyusong.com/archives/155




Android软件开发之PreferenceActivity中的组件


1.PreferenceActivity 介绍

PreferenceActivity 继承ListActivity 它是以一个列表的形式在展现内容,它最主要的特点是添加Preference可以让控件的状态持久化储存,举个例子 比如用户选中checkbox后 退出应用然后在进入应用,这时用户希望看到的是checkbox被选中,所以软件须要记录用户每次操作的过程并且持久储存,在进入应用的时候须要判断这些久储存的数据然后将系统控件的状态呈现在UI中。
尤其是软件开发肯定会有一堆设置选项选项,每次进入Activity都去手动的去取储存的数据,这样代码会变得很复杂很麻烦。 这个时候Preference就出来了,它就是专门解决这些特殊的选项保存与读取的显示。用户每次操作事件它会及时的以键值对的形式记录在SharedPreferences中,Activity每次启动它会自动帮我们完成数据的读取以及UI的显示。
android开发中一共为我们提供了4个组件,分别是CheckBoxPreference组件、EditTextPreference组件、ListPreference组件、RingtonePreference组件,下面我用一个例子一一向同学们介绍一下。




2.CheckBoxPreference组件

CheckBoxPreference 选中为true 取消选中为false 它的值会以boolean的形式储存在SharedPreferences中。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <PreferenceCategoryandroid:title="CheckBoxPreference">
  5. <CheckBoxPreferenceandroid:key="checkbox_0"
  6. android:title="CheckBox_A"
  7. android:summary="这是一个勾选框A">
  8. </CheckBoxPreference>
  9. <CheckBoxPreferenceandroid:key="checkbox_1"
  10. android:title="CheckBox_B"
  11. android:summary="这是一个勾选框B">
  12. </CheckBoxPreference>
  13. </PreferenceCategory>
  14. </PreferenceScreen>



Android软件开发之PreferenceActivity中的组件

  1. importandroid.content.Context;
  2. importandroid.os.Bundle;
  3. importandroid.preference.CheckBoxPreference;
  4. importandroid.preference.Preference;
  5. importandroid.preference.PreferenceActivity;
  6. importandroid.preference.Preference.OnPreferenceChangeListener;
  7. importandroid.preference.Preference.OnPreferenceClickListener;
  8. importandroid.widget.Toast;
  9. publicclassCheckBoxActivityextendsPreferenceActivity{
  10. ContextmContext=null;
  11. @Override
  12. protectedvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. //从资源文件中添Preferences,选择的值将会自动保存到SharePreferences
  15. addPreferencesFromResource(R.xml.checkbox);
  16. mContext=this;
  17. //CheckBoxPreference组件
  18. CheckBoxPreferencemCheckbox0=(CheckBoxPreference)findPreference("checkbox_0");
  19. mCheckbox0.setOnPreferenceClickListener(newOnPreferenceClickListener(){
  20. @Override
  21. publicbooleanonPreferenceClick(Preferencepreference){
  22. //这里可以监听到这个CheckBox的点击事件
  23. returntrue;
  24. }
  25. });
  26. mCheckbox0.setOnPreferenceChangeListener(newOnPreferenceChangeListener(){
  27. @Override
  28. publicbooleanonPreferenceChange(Preferencearg0,ObjectnewValue){
  29. //这里可以监听到checkBox中值是否改变了
  30. //并且可以拿到新改变的值
  31. Toast.makeText(mContext,"checkBox_0改变的值为"+(Boolean)newValue,Toast.LENGTH_LONG).show();
  32. returntrue;
  33. }
  34. });
  35. CheckBoxPreferencemCheckbox1=(CheckBoxPreference)findPreference("checkbox_1");
  36. mCheckbox1.setOnPreferenceClickListener(newOnPreferenceClickListener(){
  37. @Override
  38. publicbooleanonPreferenceClick(Preferencepreference){
  39. //这里可以监听到这个CheckBox的点击事件
  40. returntrue;
  41. }
  42. });
  43. mCheckbox1.setOnPreferenceChangeListener(newOnPreferenceChangeListener(){
  44. @Override
  45. publicbooleanonPreferenceChange(Preferencearg0,ObjectnewValue){
  46. //这里可以监听到checkBox中值是否改变了
  47. //并且可以拿到新改变的值
  48. Toast.makeText(mContext,"checkBox_1改变的值为"+(Boolean)newValue,Toast.LENGTH_LONG).show();
  49. returntrue;
  50. }
  51. });
  52. }
  53. }

3.EditTextPreference组件

EditTextPreference 点击后会弹出一个输入框,输入的内容会以字符串的的形式储存在SharedPreferences中。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <PreferenceCategoryandroid:title="EditTextPreference">
  5. <EditTextPreferenceandroid:key="edit_0"
  6. android:title="输入信息_A"
  7. android:summary="请输入您的信息"
  8. android:defaultValue="请输入信息"
  9. android:dialogTitle="输入框">
  10. </EditTextPreference>
  11. <EditTextPreferenceandroid:key="edit_1"
  12. android:title="输入信息_B"
  13. android:summary="请输入您的信息"
  14. android:defaultValue="请输入信息"
  15. android:dialogTitle="输入框">
  16. </EditTextPreference>
  17. </PreferenceCategory>
  18. </PreferenceScreen>

Android软件开发之PreferenceActivity中的组件Android软件开发之PreferenceActivity中的组件

  1. importandroid.content.Context;
  2. importandroid.os.Bundle;
  3. importandroid.preference.EditTextPreference;
  4. importandroid.preference.PreferenceActivity;
  5. publicclassEditTextActivityextendsPreferenceActivity{
  6. ContextmContext=null;
  7. @Override
  8. protectedvoidonCreate(BundlesavedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. //从资源文件中添Preferences,选择的值将会自动保存到SharePreferences
  11. addPreferencesFromResource(R.xml.edittext);
  12. mContext=this;
  13. //EditTextPreference组件
  14. EditTextPreferencemEditText=(EditTextPreference)findPreference("edit_0");
  15. //设置dialog按钮信息
  16. mEditText.setPositiveButtonText("确定");
  17. mEditText.setNegativeButtonText("取消");
  18. //设置按钮图标
  19. mEditText.setDialogIcon(R.drawable.jay);
  20. }
  21. }

4.ListPreference组件

在res/array中先写两个数组,一个用与list的显示内容,一个用户list的选中数值。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <string-arrayname="auto_logout_time_key">
  4. <item>10mins.</item>
  5. <item>20mins.</item>
  6. <item>30mins.</item>
  7. <item>60mins.</item>
  8. </string-array>
  9. <string-arrayname="auto_logout_time_value">
  10. <item>600000</item>
  11. <item>1200000</item>
  12. <item>1800000</item>
  13. <item>3600000</item>
  14. </string-array>
  15. </resources>

ListPreference点击后会弹出一个列表框,选中后会将选中的内容(上面数组中的值)会以字符串的的形式储存在SharedPreferences中。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <PreferenceCategoryandroid:title="ListPreference">
  5. <ListPreference
  6. android:key="list_0"
  7. android:title="登录设置A"
  8. android:dialogTitle="选择在线时间"
  9. android:entries="@array/auto_logout_time_key"
  10. android:entryValues="@array/auto_logout_time_value">
  11. </ListPreference>
  12. <ListPreference
  13. android:key="list_0"
  14. android:title="登录设置A"
  15. android:dialogTitle="选择在线时间"
  16. android:entries="@array/auto_logout_time_key"
  17. android:entryValues="@array/auto_logout_time_value">
  18. </ListPreference>
  19. </PreferenceCategory>
  20. </PreferenceScreen>

Android软件开发之PreferenceActivity中的组件

  1. importandroid.os.Bundle;
  2. importandroid.preference.PreferenceActivity;
  3. publicclassListActivityextendsPreferenceActivity{
  4. @Override
  5. protectedvoidonCreate(BundlesavedInstanceState){
  6. super.onCreate(savedInstanceState);
  7. //从资源文件中添Preferences,选择的值将会自动保存到SharePreferences
  8. addPreferencesFromResource(R.xml.list);
  9. }
  10. }

5.RingtonePreference组件

RingtonePreference点击后会弹出一个系统铃声的列表框,选中后会将选中的内容(uri字符集)会以字符串的的形式储存在SharedPreferences中。


  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <PreferenceCategoryandroid:title="RingtonePreference">
  5. <RingtonePreference
  6. android:key="ringtone_0"
  7. android:summary="选择系统铃声A"
  8. android:title="铃声设置"
  9. android:ringtoneType="all"
  10. android:showSilent="true"></RingtonePreference>
  11. <RingtonePreference
  12. android:key="ringtone_!"
  13. android:summary="选择系统铃声B"
  14. android:title="铃声设置"
  15. android:ringtoneType="all"
  16. android:showSilent="true"></RingtonePreference>
  17. </PreferenceCategory>
  18. </PreferenceScreen>

android:ringtoneType 系统一共提供了4中响铃模式的类型分别为 铃声(ringtone) 通知( notification) 警告(alarm) 全部(all)

模拟器默认是没有铃声的,下图中的铃声我是将歌曲文件拷贝到SD卡中,设置铃声后才会出现的。如果觉得拷贝麻烦可以使用豌豆荚或者91助手将歌曲文件放入手机SD卡中,在铃声设置那里设置一下在这里就会出现。





Android软件开发之PreferenceActivity中的组件

  1. importandroid.os.Bundle;
  2. importandroid.preference.PreferenceActivity;
  3. publicclassRingtoneActivityextendsPreferenceActivity{
  4. @Override
  5. protectedvoidonCreate(BundlesavedInstanceState){
  6. super.onCreate(savedInstanceState);
  7. //从资源文件中添Preferences,选择的值将会自动保存到SharePreferences
  8. addPreferencesFromResource(R.xml.ringtone);
  9. }
  10. }


5.自定义控件

使用系统的控件在显示方面难免会有些单一,如果想做一个好看的界面就需要使用自定义Preference。下面我简单说明一下如何编写自定义Preference。首先在res/layout中添加preferences文件

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#00000000">
  6. <LinearLayout
  7. android:gravity="center_vertical"
  8. android:background="@drawable/preference_mid_background"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. >
  12. <ImageView
  13. android:focusable="false"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"android:src="@drawable/setting_about_us">
  16. </ImageView>
  17. <RelativeLayout
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_marginLeft="15dip"
  21. android:layout_marginTop="6dip"
  22. android:layout_marginRight="6dip"
  23. android:layout_marginBottom="6dip"
  24. android:layout_weight="1"
  25. >
  26. <TextView
  27. android:textSize="15dip"
  28. android:textColor="#000000"
  29. android:ellipsize="marquee"
  30. android:id="@+android:id/title"
  31. android:fadingEdge="horizontal"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:singleLine="true"
  35. >
  36. </TextView>
  37. <TextView
  38. android:textAppearance="?android:attr/textAppearanceSmall"
  39. android:textColor="#565656"
  40. android:id="@+android:id/summary"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:maxLines="4"
  44. android:layout_below="@+android:id/title"
  45. android:layout_alignLeft="@+android:id/title"
  46. >
  47. </TextView>
  48. </RelativeLayout>
  49. <ImageView
  50. android:focusable="false"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:background="@drawable/preference_arrows"/>
  54. </LinearLayout>
  55. </LinearLayout>

android:background="@drawable/preference_mid_background"
通过这一行可以设置这个按钮的点击、选中默认的显示状态,这样可以让你的按钮更加好看。须要在res/drawable中添加xml文件

android:state_facused :为控件选中显示
android:state_pressed:为控件按下显示
最后一个为默认显示



  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <selector
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <item
  5. android:state_focused="true"
  6. android:drawable="@drawable/preference_mid_pressed"
  7. >
  8. </item>
  9. <item
  10. android:state_pressed="true"
  11. android:drawable="@drawable/preference_mid_pressed"
  12. >
  13. </item>
  14. <item
  15. android:drawable="@drawable/preference_mid"
  16. >
  17. </item>
  18. </selector>


Android软件开发之PreferenceActivity中的组件

  1. importandroid.content.Context;
  2. importandroid.os.Bundle;
  3. importandroid.preference.Preference;
  4. importandroid.preference.PreferenceActivity;
  5. importandroid.preference.Preference.OnPreferenceClickListener;
  6. importandroid.widget.Toast;
  7. publicclassAllActivityextendsPreferenceActivity{
  8. /**自定义布局A**/
  9. Preferencepreference0=null;
  10. /**自定义布局B**/
  11. Preferencepreference1=null;
  12. ContextmContext=null;
  13. @Override
  14. protectedvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. //从资源文件中添Preferences,选择的值将会自动保存到SharePreferences
  17. addPreferencesFromResource(R.xml.all);
  18. mContext=this;
  19. preference0=findPreference("pref_key_0");
  20. preference0.setOnPreferenceClickListener(newOnPreferenceClickListener(){
  21. @Override
  22. publicbooleanonPreferenceClick(Preferencepreference){
  23. Toast.makeText(mContext,"自定义布局A被按下",Toast.LENGTH_LONG).show();
  24. returnfalse;
  25. }
  26. });
  27. preference1=findPreference("pref_key_1");
  28. preference1.setOnPreferenceClickListener(newOnPreferenceClickListener(){
  29. @Override
  30. publicbooleanonPreferenceClick(Preferencepreference){
  31. Toast.makeText(mContext,"自定义布局B被按下",Toast.LENGTH_LONG).show();
  32. returnfalse;
  33. }
  34. });
  35. }
  36. }

读取数据

在PreferenceActivity中可以用下面这种方式拿到SharedPreferences中储存的数值,通过PreferenceManager.getDefaultSharedPreferences(this) 方法拿到控件默认储存的sharedPreferences对象。


  1. SharedPreferencesprefs=PreferenceManager.getDefaultSharedPreferences(this);
  2. booleansomething=prefs.getBoolean("something",false);




在模拟起中将SharedPreferences储存内容拷贝出来后,可以清楚的看到通过点击系统控件储存的数值。这里我说一下铃声的储存,它是以一个字符串形式的uri字符集,它所指向的是系统铃声储存的路径。所以根据这个字符集就可以找到这个铃声。


  1. <?xmlversion='1.0'encoding='utf-8'standalone='yes'?>
  2. <map>
  3. <stringname="ringtone_!">content://media/external/audio/media/1</string>
  4. <stringname="ringtone_0">content://media/external/audio/media/1</string>
  5. <stringname="list_0">1800000</string>
  6. <stringname="edit_1">请输入信息1212</string>
  7. <stringname="list">1200000</string>
  8. <stringname="ringtone">content://settings/system/ringtone</string>
  9. <booleanname="checkbox_0"value="true"/>
  10. <booleanname="checkbox_1"value="true"/>
  11. <stringname="edit_0">请输入信息</string>
  12. </map>





今天出去散了散心,心情挺开心的,哇咔咔~~总的来说这一篇在实际的软件开发中还是非常重要的,老规矩每篇文章都会附带源代码,最后如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习雨松MOMO希望可以和大家一起进步。


下载地址:http://download.****.net/source/3556196