首先在res文件中创建一个xml文件夹,创建preference.xml文件,然后写布局:
1 <?xml version="1.0" encoding="utf-8"?> 2 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 3 4 <PreferenceCategory android:title="系统信息" > 5 <EditTextPreference 6 android:defaultValue="机主姓名" 7 android:icon="@drawable/ic_launcher" 8 android:key="edit_text_key_1" 9 android:summary="木子小新" 10 android:title="机主姓名" /> 11 12 <ListPreference 13 android:dialogIcon="@drawable/ic_launcher" 14 android:dialogTitle="城市区号" 15 android:entries="@array/city" 16 android:entryValues="@array/city_quhao" 17 android:icon="@drawable/ic_launcher" 18 android:key="listpreference" 19 android:title="区号" /> 20 </PreferenceCategory> 21 <PreferenceCategory android:title="系统设置" > 22 <Preference 23 android:icon="@drawable/bokeyuan" 24 android:summary="http://www.cnblogs.com/labixiaoxin/" 25 android:title="机主博客" > 26 <intent 27 android:action="android.intent.action.VIEW" 28 android:data="http://www.cnblogs.com/labixiaoxin/" /> 29 </Preference> 30 31 <SwitchPreference 32 android:icon="@drawable/wifi" 33 android:key="wifi" 34 android:title="wifi开关" /> 35 <SwitchPreference 36 android:icon="@drawable/lanya" 37 android:key="lanya" 38 android:title="蓝牙开关" /> 39 40 <CheckBoxPreference 41 android:defaultValue="true" 42 android:icon="@drawable/xuanzhuahn" 43 android:key="checkbox_key_xuanzhuan" 44 android:title="旋转" /> 45 46 <RingtonePreference 47 android:icon="@drawable/ring" 48 android:key="ring" 49 android:title="铃声" /> 50 </PreferenceCategory> 51 52 <PreferenceScreen 53 android:icon="@drawable/ic_launcher" 54 android:key="preference_screen_2_key" 55 android:persistent="false" 56 android:title="其他设置" > 57 <CheckBoxPreference 58 android:defaultValue="true" 59 android:icon="@drawable/ic_launcher" 60 android:key="checkbox_key_wangluo" 61 android:summary="网络已经开启" 62 android:title="网络" /> 63 </PreferenceScreen> 64 65 </PreferenceScreen>
从主Acitivity中加载SettingFragment:
1 package com.lixu.android_set; 2 3 import android.app.Activity; 4 import android.app.FragmentManager; 5 import android.app.FragmentTransaction; 6 import android.os.Bundle; 7 8 public class MainActivity extends Activity { 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 // 创建Fragment管理器 14 FragmentManager fm = this.getFragmentManager(); 15 FragmentTransaction ft = fm.beginTransaction(); 16 // 将原有的Activity替换成Fragment。也可以添加到原有之上不覆盖 如:ft.add(R.id.rel, new 17 // Fragment()) ;如果添加的多了会比较耗资源。 18 ft.replace(android.R.id.content, new SettingFragment()); 19 // 提交 20 ft.commit(); 21 22 } 23 }
1 package com.lixu.android_set; 2 3 import android.content.SharedPreferences; 4 import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 5 import android.os.Bundle; 6 import android.preference.CheckBoxPreference; 7 import android.preference.EditTextPreference; 8 import android.preference.ListPreference; 9 import android.preference.PreferenceFragment; 10 import android.preference.PreferenceManager; 11 import android.preference.SwitchPreference; 12 13 //安卓官方申明在3.0版本后最好不要用FragmentActivity 应该用PreferenceFragment,在从主Activity中调用。 14 public class SettingFragment extends PreferenceFragment { 15 16 private MyListener mMyListener = new MyListener(); 17 private EditTextPreference mEditTextPreference; 18 private ListPreference mListPreference; 19 private CheckBoxPreference mCheckBoxPreference; 20 private SwitchPreference mSwitchPreference1; 21 private SwitchPreference mSwitchPreference2; 22 23 @Override 24 public void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 // 读取值的通用方法 27 SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 28 29 // 添加设置选项 30 addPreferencesFromResource(R.xml.preferences); 31 // 找到各个控件按钮 32 mEditTextPreference = (EditTextPreference) findPreference("edit_text_key_1"); 33 34 mListPreference = (ListPreference) findPreference("listpreference"); 35 36 mCheckBoxPreference = (CheckBoxPreference) findPreference("checkbox_key_xuanzhuan"); 37 38 mSwitchPreference1 = (SwitchPreference) findPreference("wifi"); 39 mSwitchPreference2 = (SwitchPreference) findPreference("lanya"); 40 // 根据实际情况设置提示框的初始状态 41 boolean s1 = sharedpreferences.getBoolean("wifi", false); 42 if (!s1) 43 mSwitchPreference1.setSummary("wifi关闭"); 44 else if (s1) 45 mSwitchPreference1.setSummary("wifi开启"); 46 47 boolean s2 = sharedpreferences.getBoolean("lanya", false); 48 if (!s2) 49 mSwitchPreference2.setSummary("蓝牙关闭"); 50 else if (s2) 51 mSwitchPreference2.setSummary("蓝牙开启"); 52 53 boolean s = sharedpreferences.getBoolean("checkbox_key_xuanzhuan", false); 54 if (!s) 55 mCheckBoxPreference.setSummary("旋转关闭"); 56 else if (s) 57 mCheckBoxPreference.setSummary("旋转开启"); 58 59 // 添加项目数据发生变化时候的监听 60 sharedpreferences.unregisterOnSharedPreferenceChangeListener(mMyListener); 61 } 62 63 // 程序暂停时取消注册监听事件,使得代码更加完整。 64 @Override 65 public void onPause() { 66 super.onPause(); 67 getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(mMyListener); 68 } 69 70 // 程序运行时注册 71 @Override 72 public void onResume() { 73 super.onResume(); 74 getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(mMyListener); 75 } 76 77 private class MyListener implements OnSharedPreferenceChangeListener { 78 79 @Override 80 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 81 82 switch (key) { 83 case "edit_text_key_1": 84 String str1 = sharedPreferences.getString("edit_text_key_1", "null"); 85 mEditTextPreference.setSummary(str1); 86 87 break; 88 case "listpreference": 89 String city = mListPreference.getValue(); 90 String city_num = mListPreference.getEntry() + ""; 91 mListPreference.setSummary(city + " " + city_num); 92 93 break; 94 case "checkbox_key_xuanzhuan": 95 boolean s = sharedPreferences.getBoolean("checkbox_key_xuanzhuan", false); 96 if (!s) 97 mCheckBoxPreference.setSummary("旋转关闭"); 98 else if (s) 99 mCheckBoxPreference.setSummary("旋转开启"); 100 101 break; 102 case "wifi": 103 boolean s1 = sharedPreferences.getBoolean("wifi", false); 104 if (!s1) 105 mSwitchPreference1.setSummary("wifi关闭"); 106 else if (s1) 107 mSwitchPreference1.setSummary("wifi开启"); 108 109 break; 110 case "lanya": 111 112 boolean s2 = sharedPreferences.getBoolean("lanya", false); 113 if (!s2) 114 mSwitchPreference2.setSummary("蓝牙关闭"); 115 else if (s2) 116 mSwitchPreference2.setSummary("蓝牙开启"); 117 118 break; 119 120 default: 121 break; 122 } 123 } 124 } 125 126 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string-array name="city"> 5 <item>成都</item> 6 <item>北京</item> 7 <item>上海</item> 8 </string-array> 9 <string-array name="city_quhao"> 10 <item>028</item> 11 <item>010</item> 12 <item>021</item> 13 </string-array> 14 15 </resources>
运行效果图: