你的位置:首页 > 操作系统

[操作系统]setting菜单界面的形成


 

代码:

first_preference.

 1 <??> 2 <PreferenceScreen ="http://schemas.android.com/apk/res/android" > 3  4   <PreferenceCategory android:title="第一组" > 5     <CheckBoxPreference 6       android:defaultValue="true" 7       android:icon="@drawable/ic_launcher" 8       android:key="checkbox_key1" 9       android:summary="true"10       android:title="CheckBoxPreference" />11 12     <EditTextPreference13       android:defaultValue="小米"14       android:icon="@drawable/ic_launcher"15       android:key="edit_key1"16       android:summary="小米"17       android:title="EditTextPreference" />18   </PreferenceCategory>19   <PreferenceCategory android:title="第二组" >20     <ListPreference21       android:dialogIcon="@drawable/ic_launcher"22       android:entries="@array/citys"23       android:entryValues="@array/citys_code"24       android:icon="@drawable/ic_launcher"25       android:key="listPreferenc_key"26       android:summary="圆形的选择(单项选择)ListPreference"27       android:title="ListPreference" />28 29     <!-- entries和entryValues对应关系 -->30     <MultiSelectListPreference31       android:dialogIcon="@drawable/ic_launcher"32       android:entries="@array/citys"33       android:entryValues="@array/citys_code"34       android:icon="@drawable/ic_launcher"35       android:key="multiSelectListPreference_key"36       android:summary="方形的选择(多项选择)MultiSelectListPreference"37       android:title="MultiSelectListPreference" />38   </PreferenceCategory>39 40   <RingtonePreference41     android:icon="@drawable/ic_launcher"42     android:key="RingtonePreference_key1"43     android:summary="默认铃声"44     android:title="铃声设置" >45   </RingtonePreference>46 47   <!-- 点击将浏览和打开网页 -->48   <Preference49     android:icon="@drawable/ic_launcher"50     android:summary="http://www.baidu.com"51     android:title="浏览网页" >52     <intent53       android:action="android.intent.action.VIEW"54       android:data="http://www.baidu.com" />55   </Preference>56 57   <SwitchPreference58     android:defaultValue="false"59     android:icon="@drawable/ic_launcher"60     android:key="switchPreference_key1"61     android:summary="wifi开关"62     android:title="wifi" />63 64   <!-- 此处将跳转到下一屏设置 -->65   <PreferenceScreen66     android:icon="@drawable/ic_launcher"67     android:key="preferenceScreen_key"68     android:persistent="false"69     android:title="其他设置" >70     <CheckBoxPreference71       android:defaultValue="false"72       android:key="checkbox_key2"73       android:summary="false"74       android:title="CheckBoxPreference" />75 76     <EditTextPreference77       android:defaultValue="小米"78       android:key="edit_key2"79       android:summary="小米"80       android:title="EditTextPreference" />81   </PreferenceScreen>82 83 </PreferenceScreen>

 

arrays.

 1 <??> 2 <resources> 3   <string-array name="citys"> 4     <item>成都</item> 5     <item>北京</item> 6     <item>上海</item> 7   </string-array> 8    9   <string-array name="citys_code"> 10     <item>028</item> 11     <item>010</item> 12     <item>021</item> 13   </string-array> 14 </resources>

 

MainActivity.java:

 1 package com.zzw.testpreferenceactivity; 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.MultiSelectListPreference; 10 import android.preference.PreferenceActivity; 11 import android.preference.PreferenceManager; 12 import android.preference.SwitchPreference; 13  14 public class MainActivity extends PreferenceActivity { 15  16   private final String CHECKBOXPREFERENCE_KEY1 = "checkbox_key1"; 17   private final String EDITTEXTPREFERENCE_KEY1 = "edit_key1"; 18   private final String LISTPREFERENCE_KEY = "listPreferenc_key"; 19   private final String MULTISELECTLISTPREFERENCE_KEY = "multiSelectListPreference_key"; 20   private final String SWITCHPREFERENCE_KEY = "switchPreference_key1"; 21  22   private SharedPreferences mSharedPreferences; 23   private OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener; 24  25   private CheckBoxPreference mCheckBoxPreference; 26   private EditTextPreference mEditTextPreference; 27   private ListPreference mListPreference; 28   private MultiSelectListPreference mMultiSelectListPreference; 29   private SwitchPreference mSwitchPreference; 30  31   @Override 32   protected void onCreate(Bundle savedInstanceState) { 33     super.onCreate(savedInstanceState); 34  35     // 添加设置的选项 36     addPreferencesFromResource(R. 37  38     mCheckBoxPreference = (CheckBoxPreference) findPreference(CHECKBOXPREFERENCE_KEY1); 39     mEditTextPreference = (EditTextPreference) findPreference(EDITTEXTPREFERENCE_KEY1); 40     mListPreference = (ListPreference) findPreference(LISTPREFERENCE_KEY); 41     mMultiSelectListPreference = (MultiSelectListPreference) findPreference(MULTISELECTLISTPREFERENCE_KEY); 42     mSwitchPreference = (SwitchPreference) findPreference(SWITCHPREFERENCE_KEY); 43  44     // 获取SharedPreferences对象 45     mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 46  47     // 通用的读取设置的某个值的方法 48     // boolean b = mSharedPreferences.getBoolean("checkbox_key1", false); 49     // Log.d("checkbox_key1", b+""); 50     // 51     // String s = mSharedPreferences.getString("edit_key1", "暂时为空"); 52     // Log.d("edit_key1", s); 53  54     mOnSharedPreferenceChangeListener = new MyOnSharedPreferenceChangeListener(); 55   } 56  57   // 数据发生变化时候的监听类设置 58   private class MyOnSharedPreferenceChangeListener implements OnSharedPreferenceChangeListener { 59  60     @Override 61     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 62  63       if (CHECKBOXPREFERENCE_KEY1.equals(key)) { 64  65         boolean b = sharedPreferences.getBoolean(CHECKBOXPREFERENCE_KEY1, false); 66         mCheckBoxPreference.setSummary(b + ""); 67         // Toast.makeText(getApplicationContext(), "checkBox变化了,当前值--" + 68         // b, 0).show(); 69  70       } else if (EDITTEXTPREFERENCE_KEY1.equals(key)) { 71  72         String s = sharedPreferences.getString(EDITTEXTPREFERENCE_KEY1, "null"); 73         mEditTextPreference.setSummary(s); 74         // Toast.makeText(getApplicationContext(), "editText变化了,当前值--" + 75         // s, 0).show(); 76  77       } else if (LISTPREFERENCE_KEY.equals(key)) { 78  79         String e = mListPreference.getEntry() + ""; 80         String v = mListPreference.getValue(); 81         mListPreference.setSummary(e + " : " + v); 82  83       } else if (MULTISELECTLISTPREFERENCE_KEY.equals(key)) { 84         // String s_entries="entries:"; 85         // 86         // CharSequence 87         // []entries=mMultiSelectListPreference.getEntries(); 88         // for(CharSequence c:entries){ 89         // s_entries+=c+""+"\t"; 90         // } 91         // 92         // Set<String> values=mMultiSelectListPreference.getValues(); 93         // Iterator<String> it=values.iterator(); 94         // String s_values="values:"; 95         // while(it.hasNext()){ 96         // s_values+=it.next().toString()+"\t"; 97         // } 98         // mMultiSelectListPreference.setSummary(s_entries+"\n"+s_values); 99 100       } else if (SWITCHPREFERENCE_KEY.equals(key)) {101 102         boolean b = sharedPreferences.getBoolean(SWITCHPREFERENCE_KEY, false);103         mSwitchPreference.setSummary(b + "");104       }105     }106   }107 108   @Override109   protected void onResume() {110     super.onResume();111     // 注册数据发生变化时候的监听112     mSharedPreferences.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);113     // getPreferenceScreen().getSharedPreferences()114     // .registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);115   }116 117   @Override118   protected void onPause() {119     super.onPause();120     // 取消数据发生变化时候的监听121     mSharedPreferences.unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);122     // getPreferenceScreen().getSharedPreferences()123     // .unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);124   }125 126 }