本文是上一篇文章的优化版:
上文链接地址:http://www.cnblogs.com/zzw1994/p/5016864.html
上文中有很多方法都是过时,并且效率不是很高,主要对JAVA代码进行了优化,下面是优化后的代码:
MainActivity.java:
package com.zzw.testpreferenceactivity; import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); this.getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingFragment()).commit();
} }
SettingFragment.java:
package com.zzw.testpreferenceactivity; import java.util.Iterator;
import java.util.Set; import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.MultiSelectListPreference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.widget.Toast; public class SettingFragment extends PreferenceFragment { private final String CHECKBOXPREFERENCE_KEY1 = "checkbox_key1";
private final String EDITTEXTPREFERENCE_KEY1 = "edit_key1";
private final String LISTPREFERENCE_KEY = "listPreferenc_key";
private final String MULTISELECTLISTPREFERENCE_KEY = "multiSelectListPreference_key";
private final String SWITCHPREFERENCE_KEY = "switchPreference_key1"; private SharedPreferences mSharedPreferences;
private OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener; private CheckBoxPreference mCheckBoxPreference;
private EditTextPreference mEditTextPreference;
private ListPreference mListPreference;
private MultiSelectListPreference mMultiSelectListPreference;
private SwitchPreference mSwitchPreference; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 添加设置的选项
addPreferencesFromResource(R.xml.first_preference); mCheckBoxPreference = (CheckBoxPreference) findPreference(CHECKBOXPREFERENCE_KEY1);
mEditTextPreference = (EditTextPreference) findPreference(EDITTEXTPREFERENCE_KEY1);
mListPreference = (ListPreference) findPreference(LISTPREFERENCE_KEY);
mMultiSelectListPreference = (MultiSelectListPreference) findPreference(MULTISELECTLISTPREFERENCE_KEY);
mSwitchPreference = (SwitchPreference) findPreference(SWITCHPREFERENCE_KEY); // 获取SharedPreferences对象
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); // 通用的读取设置的某个值的方法
// boolean b = mSharedPreferences.getBoolean("checkbox_key1", false);
// Log.d("checkbox_key1", b+"");
//
// String s = mSharedPreferences.getString("edit_key1", "暂时为空");
// Log.d("edit_key1", s); mOnSharedPreferenceChangeListener = new MyOnSharedPreferenceChangeListener();
} // 数据发生变化时候的监听类设置
private class MyOnSharedPreferenceChangeListener implements OnSharedPreferenceChangeListener { @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) {
case CHECKBOXPREFERENCE_KEY1: boolean b = sharedPreferences.getBoolean(CHECKBOXPREFERENCE_KEY1, false);
mCheckBoxPreference.setSummary(b + "");
break; case EDITTEXTPREFERENCE_KEY1: String s = sharedPreferences.getString(EDITTEXTPREFERENCE_KEY1, "null");
mEditTextPreference.setSummary(s);
break; case LISTPREFERENCE_KEY: String e = mListPreference.getEntry() + "";
String v = mListPreference.getValue();
mListPreference.setSummary(e + " : " + v);
break; case MULTISELECTLISTPREFERENCE_KEY: // String s_entries = "entries:";
//
// CharSequence[] entries = mMultiSelectListPreference.getEntries();
// for (CharSequence c : entries) {
// s_entries += c + "" + "\t";
// }
//
// Set<String> values = mMultiSelectListPreference.getValues();
// Iterator<String> it = values.iterator();
// String s_values = "values:";
// while (it.hasNext()) {
// s_values += it.next().toString() + "\t";
// }
// mMultiSelectListPreference.setSummary(s_entries + "\n" + s_values);
break; case SWITCHPREFERENCE_KEY: boolean boo = sharedPreferences.getBoolean(SWITCHPREFERENCE_KEY, false);
mSwitchPreference.setSummary(boo + ""); break;
default:
break;
}
}
} @Override
public void onResume() {
super.onResume();
// 注册数据发生变化时候的监听
mSharedPreferences.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
// getPreferenceScreen().getSharedPreferences()
// .registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
} @Override
public void onPause() {
super.onPause();
// 取消数据发生变化时候的监听
mSharedPreferences.unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
// getPreferenceScreen().getSharedPreferences()
// .unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
}
}
主要优化的地方有:①、使用了switch,提高了效率(Jdk1.7以上,包括1.7)
②、把seeting菜单改变时的监听提取了出来,降低了代码的耦合度