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

时间:2022-09-19 05:06:03

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

代码:

first_preference.xml:

 <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="第一组" >
<CheckBoxPreference
android:defaultValue="true"
android:icon="@drawable/ic_launcher"
android:key="checkbox_key1"
android:summary="true"
android:title="CheckBoxPreference" /> <EditTextPreference
android:defaultValue="小米"
android:icon="@drawable/ic_launcher"
android:key="edit_key1"
android:summary="小米"
android:title="EditTextPreference" />
</PreferenceCategory>
<PreferenceCategory android:title="第二组" >
<ListPreference
android:dialogIcon="@drawable/ic_launcher"
android:entries="@array/citys"
android:entryValues="@array/citys_code"
android:icon="@drawable/ic_launcher"
android:key="listPreferenc_key"
android:summary="圆形的选择(单项选择)ListPreference"
android:title="ListPreference" /> <!-- entries和entryValues对应关系 -->
<MultiSelectListPreference
android:dialogIcon="@drawable/ic_launcher"
android:entries="@array/citys"
android:entryValues="@array/citys_code"
android:icon="@drawable/ic_launcher"
android:key="multiSelectListPreference_key"
android:summary="方形的选择(多项选择)MultiSelectListPreference"
android:title="MultiSelectListPreference" />
</PreferenceCategory> <RingtonePreference
android:icon="@drawable/ic_launcher"
android:key="RingtonePreference_key1"
android:summary="默认铃声"
android:title="铃声设置" >
</RingtonePreference> <!-- 点击将浏览和打开网页 -->
<Preference
android:icon="@drawable/ic_launcher"
android:summary="http://www.baidu.com"
android:title="浏览网页" >
<intent
android:action="android.intent.action.VIEW"
android:data="http://www.baidu.com" />
</Preference> <SwitchPreference
android:defaultValue="false"
android:icon="@drawable/ic_launcher"
android:key="switchPreference_key1"
android:summary="wifi开关"
android:title="wifi" /> <!-- 此处将跳转到下一屏设置 -->
<PreferenceScreen
android:icon="@drawable/ic_launcher"
android:key="preferenceScreen_key"
android:persistent="false"
android:title="其他设置" >
<CheckBoxPreference
android:defaultValue="false"
android:key="checkbox_key2"
android:summary="false"
android:title="CheckBoxPreference" /> <EditTextPreference
android:defaultValue="小米"
android:key="edit_key2"
android:summary="小米"
android:title="EditTextPreference" />
</PreferenceScreen> </PreferenceScreen>

arrays.xml:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="citys">
<item>成都</item>
<item>北京</item>
<item>上海</item>
</string-array> <string-array name="citys_code">
<item>028</item>
<item>010</item>
<item>021</item>
</string-array>
</resources>

MainActivity.java:

 package com.zzw.testpreferenceactivity;

 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.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference; public class MainActivity extends PreferenceActivity { 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
protected 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(getApplicationContext()); // 通用的读取设置的某个值的方法
// 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) { if (CHECKBOXPREFERENCE_KEY1.equals(key)) { boolean b = sharedPreferences.getBoolean(CHECKBOXPREFERENCE_KEY1, false);
mCheckBoxPreference.setSummary(b + "");
// Toast.makeText(getApplicationContext(), "checkBox变化了,当前值--" +
// b, 0).show(); } else if (EDITTEXTPREFERENCE_KEY1.equals(key)) { String s = sharedPreferences.getString(EDITTEXTPREFERENCE_KEY1, "null");
mEditTextPreference.setSummary(s);
// Toast.makeText(getApplicationContext(), "editText变化了,当前值--" +
// s, 0).show(); } else if (LISTPREFERENCE_KEY.equals(key)) { String e = mListPreference.getEntry() + "";
String v = mListPreference.getValue();
mListPreference.setSummary(e + " : " + v); } else if (MULTISELECTLISTPREFERENCE_KEY.equals(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); } else if (SWITCHPREFERENCE_KEY.equals(key)) { boolean b = sharedPreferences.getBoolean(SWITCHPREFERENCE_KEY, false);
mSwitchPreference.setSummary(b + "");
}
}
} @Override
protected void onResume() {
super.onResume();
// 注册数据发生变化时候的监听
mSharedPreferences.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
// getPreferenceScreen().getSharedPreferences()
// .registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
} @Override
protected void onPause() {
super.onPause();
// 取消数据发生变化时候的监听
mSharedPreferences.unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
// getPreferenceScreen().getSharedPreferences()
// .unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
} }