Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> seeting菜單界面形成--優化,seeting菜單界面--

seeting菜單界面形成--優化,seeting菜單界面--

編輯:關於android開發

seeting菜單界面形成--優化,seeting菜單界面--


本文是上一篇文章的優化版:

上文鏈接地址:http://www.cnblogs.com/zzw1994/p/5016864.html

上文中有很多方法都是過時,並且效率不是很高,主要對JAVA代碼進行了優化,下面是優化後的代碼:

 

MainActivity.java:

 1 package com.zzw.testpreferenceactivity;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 
 6 public class MainActivity extends Activity {
 7 
 8     @Override
 9     protected void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11 
12         this.getFragmentManager().beginTransaction()
13         .replace(android.R.id.content, new SettingFragment()).commit();
14     }
15 
16 }

 

SettingFragment.java:

  1 package com.zzw.testpreferenceactivity;
  2 
  3 import java.util.Iterator;
  4 import java.util.Set;
  5 
  6 import android.content.SharedPreferences;
  7 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  8 import android.os.Bundle;
  9 import android.preference.CheckBoxPreference;
 10 import android.preference.EditTextPreference;
 11 import android.preference.ListPreference;
 12 import android.preference.MultiSelectListPreference;
 13 import android.preference.PreferenceFragment;
 14 import android.preference.PreferenceManager;
 15 import android.preference.SwitchPreference;
 16 import android.widget.Toast;
 17 
 18 public class SettingFragment extends PreferenceFragment {
 19 
 20     private final String CHECKBOXPREFERENCE_KEY1 = "checkbox_key1";
 21     private final String EDITTEXTPREFERENCE_KEY1 = "edit_key1";
 22     private final String LISTPREFERENCE_KEY = "listPreferenc_key";
 23     private final String MULTISELECTLISTPREFERENCE_KEY = "multiSelectListPreference_key";
 24     private final String SWITCHPREFERENCE_KEY = "switchPreference_key1";
 25 
 26     private SharedPreferences mSharedPreferences;
 27     private OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener;
 28 
 29     private CheckBoxPreference mCheckBoxPreference;
 30     private EditTextPreference mEditTextPreference;
 31     private ListPreference mListPreference;
 32     private MultiSelectListPreference mMultiSelectListPreference;
 33     private SwitchPreference mSwitchPreference;
 34 
 35     @Override
 36     public void onCreate(Bundle savedInstanceState) {
 37         super.onCreate(savedInstanceState);
 38 
 39         // 添加設置的選項
 40         addPreferencesFromResource(R.xml.first_preference);
 41 
 42         mCheckBoxPreference = (CheckBoxPreference) findPreference(CHECKBOXPREFERENCE_KEY1);
 43         mEditTextPreference = (EditTextPreference) findPreference(EDITTEXTPREFERENCE_KEY1);
 44         mListPreference = (ListPreference) findPreference(LISTPREFERENCE_KEY);
 45         mMultiSelectListPreference = (MultiSelectListPreference) findPreference(MULTISELECTLISTPREFERENCE_KEY);
 46         mSwitchPreference = (SwitchPreference) findPreference(SWITCHPREFERENCE_KEY);
 47 
 48         // 獲取SharedPreferences對象
 49         mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
 50 
 51         // 通用的讀取設置的某個值的方法
 52         // boolean b = mSharedPreferences.getBoolean("checkbox_key1", false);
 53         // Log.d("checkbox_key1", b+"");
 54         //
 55         // String s = mSharedPreferences.getString("edit_key1", "暫時為空");
 56         // Log.d("edit_key1", s);
 57 
 58         mOnSharedPreferenceChangeListener = new MyOnSharedPreferenceChangeListener();
 59     }
 60 
 61     // 數據發生變化時候的監聽類設置
 62     private class MyOnSharedPreferenceChangeListener implements OnSharedPreferenceChangeListener {
 63 
 64         @Override
 65         public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
 66             switch (key) {
 67             case CHECKBOXPREFERENCE_KEY1:
 68 
 69                 boolean b = sharedPreferences.getBoolean(CHECKBOXPREFERENCE_KEY1, false);
 70                 mCheckBoxPreference.setSummary(b + "");
 71                 break;
 72 
 73             case EDITTEXTPREFERENCE_KEY1:
 74 
 75                 String s = sharedPreferences.getString(EDITTEXTPREFERENCE_KEY1, "null");
 76                 mEditTextPreference.setSummary(s);
 77                 break;
 78 
 79             case LISTPREFERENCE_KEY:
 80 
 81                 String e = mListPreference.getEntry() + "";
 82                 String v = mListPreference.getValue();
 83                 mListPreference.setSummary(e + " : " + v);
 84                 break;
 85 
 86             case MULTISELECTLISTPREFERENCE_KEY:
 87 
 88 //                String s_entries = "entries:";
 89 //
 90 //                CharSequence[] entries = mMultiSelectListPreference.getEntries();
 91 //                for (CharSequence c : entries) {
 92 //                    s_entries += c + "" + "\t";
 93 //                }
 94 //
 95 //                Set<String> values = mMultiSelectListPreference.getValues();
 96 //                Iterator<String> it = values.iterator();
 97 //                String s_values = "values:";
 98 //                while (it.hasNext()) {
 99 //                    s_values += it.next().toString() + "\t";
100 //                }
101 //                mMultiSelectListPreference.setSummary(s_entries + "\n" + s_values);
102                 break;
103 
104             case SWITCHPREFERENCE_KEY:
105                 
106                 boolean boo = sharedPreferences.getBoolean(SWITCHPREFERENCE_KEY, false);
107                 mSwitchPreference.setSummary(boo + "");
108                 
109                 break;
110             default:
111                 break;
112             }
113         }
114     }
115 
116     @Override
117     public void onResume() {
118         super.onResume();
119         // 注冊數據發生變化時候的監聽
120         mSharedPreferences.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
121         // getPreferenceScreen().getSharedPreferences()
122         // .registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
123     }
124 
125     @Override
126     public void onPause() {
127         super.onPause();
128         // 取消數據發生變化時候的監聽
129         mSharedPreferences.unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
130         // getPreferenceScreen().getSharedPreferences()
131         // .unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
132     }
133 }

 

主要優化的地方有:①、使用了switch,提高了效率(Jdk1.7以上,包括1.7)

         ②、把seeting菜單改變時的監聽提取了出來,降低了代碼的耦合度

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved