• setting菜单界面的形成--未优化


    代码:

    first_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         <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         <EditTextPreference
    13             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         <ListPreference
    21             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         <MultiSelectListPreference
    31             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     <RingtonePreference
    41         android:icon="@drawable/ic_launcher"
    42         android:key="RingtonePreference_key1"
    43         android:summary="默认铃声"
    44         android:title="铃声设置" >
    45     </RingtonePreference>
    46 
    47     <!-- 点击将浏览和打开网页 -->
    48     <Preference
    49         android:icon="@drawable/ic_launcher"
    50         android:summary="http://www.baidu.com"
    51         android:title="浏览网页" >
    52         <intent
    53             android:action="android.intent.action.VIEW"
    54             android:data="http://www.baidu.com" />
    55     </Preference>
    56 
    57     <SwitchPreference
    58         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     <PreferenceScreen
    66         android:icon="@drawable/ic_launcher"
    67         android:key="preferenceScreen_key"
    68         android:persistent="false"
    69         android:title="其他设置" >
    70         <CheckBoxPreference
    71             android:defaultValue="false"
    72             android:key="checkbox_key2"
    73             android:summary="false"
    74             android:title="CheckBoxPreference" />
    75 
    76         <EditTextPreference
    77             android:defaultValue="小米"
    78             android:key="edit_key2"
    79             android:summary="小米"
    80             android:title="EditTextPreference" />
    81     </PreferenceScreen>
    82 
    83 </PreferenceScreen>

    arrays.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     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.xml.first_preference);
     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+""+"	";
     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()+"	";
     97                 // }
     98                 // mMultiSelectListPreference.setSummary(s_entries+"
    "+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     @Override
    109     protected void onResume() {
    110         super.onResume();
    111         // 注册数据发生变化时候的监听
    112         mSharedPreferences.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
    113         // getPreferenceScreen().getSharedPreferences()
    114         // .registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
    115     }
    116 
    117     @Override
    118     protected void onPause() {
    119         super.onPause();
    120         // 取消数据发生变化时候的监听
    121         mSharedPreferences.unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
    122         // getPreferenceScreen().getSharedPreferences()
    123         // .unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
    124     }
    125 
    126 }
  • 相关阅读:
    1203正规式转换为有穷自动机
    访问本班同学的博客
    0312复利计算3
    操作系统
    0311复利计算2
    0309简单的复利计算
    0302IT行业虽吃香,能完全享受这块“香"的也很难
    12.30递归下降语义分析
    1203正规式转换为有穷自动机
    对10位同学的文法解释和语法树的评论
  • 原文地址:https://www.cnblogs.com/zzw1994/p/5016864.html
Copyright © 2020-2023  润新知