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菜单改变时的监听提取了出来,降低了代码的耦合度