Android设置Settings:ListPreference【3】
Android的Settings实现中,ListPreference使用稍微复杂些,ListPreference使用两个数据源:entries和entryValues,entries和entryValues存在对应关系,可以这么简单理解,entries是人类可读的有意义的内容,而entryValues则是用来为机器或代码使用的具体的值,在前面文章【1】和【2】的基础写一个ListPreference的使用加以说明。
res/xml目录下的preferences.xml(有变化):
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="【第1组】" > <ListPreference android:dialogTitle="list dialog title" android:entries="@array/citys" android:entryValues="@array/citys_code" android:key="list_key" android:summary="list summary" android:title="list title" /> </PreferenceCategory> <PreferenceCategory android:title="【第2组】" > <EditTextPreference android:defaultValue="缺省值1" android:key="edit_text_key_1" android:summary="edit summary" android:title="edit text 1" /> <CheckBoxPreference android:defaultValue="true" android:key="checkbox_key" android:summary="checkbox summary" android:title="checkbox title" /> </PreferenceCategory> <!-- 点击将浏览和打开网页 --> <Preference android:summary="http://blog.csdn.net/zhangphil" android:title="浏览网页" > <intent android:action="android.intent.action.VIEW" android:data="http://blog.csdn.net/zhangphil" /> </Preference> <!-- 此处将跳转到下一屏设置 --> <PreferenceScreen android:key="preference_screen_2_key" android:persistent="false" android:title="PreferenceScreen 2" > <EditTextPreference android:defaultValue="缺省值2" android:key="edit_text_key_2" android:summary="edit summary" android:title="edit text 2" /> </PreferenceScreen> </PreferenceScreen>
需要的res/values/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>
上层Java代码:
package zhangphil.settings; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; import android.preference.PreferenceScreen; public class MainActivity extends PreferenceActivity { private MyOnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener = new MyOnSharedPreferenceChangeListener(); private EditTextPreference mEditTextPreference; private final String A_KEY = "edit_text_key_1"; private ListPreference mListPreference; private final String LIST_KEY = "list_key"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 通用的读取设置的某个值的方法 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); String s = sharedPreferences.getString(A_KEY, "暂时为空"); // 添加设置的选项 addPreferencesFromResource(R.xml.preferences); // 预读取。为PreferenceActivity中的某个设置选项初始化值 mEditTextPreference = (EditTextPreference) findPreference(A_KEY); mEditTextPreference.setSummary(s); // 注册添加设置的项数据发生变化时候的监听 sharedPreferences.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener); mListPreference = (ListPreference) findPreference(LIST_KEY); } @Override protected void onResume() { super.onResume(); getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener); } @Override protected void onPause() { super.onPause(); getPreferenceScreen().getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener); } // 那一个设置选项栏被触发 @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { return false; } private class MyOnSharedPreferenceChangeListener implements OnSharedPreferenceChangeListener { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals(A_KEY)) { // 更新 String s = sharedPreferences.getString(A_KEY, "NULL"); mEditTextPreference.setSummary(s); } if (key.equals(LIST_KEY)) { String e = mListPreference.getEntry() + ""; String v = mListPreference.getValue(); mListPreference.setSummary(e + " : " + v); } } } }
运行结果:
相关文章和文档:
【1】《Android设置Settings实现:PreferenceActivity【1】》链接地址:http://blog.csdn.net/zhangphil/article/details/50154513
【2】《Android设置Settings:预读取设置的选项和更新设置结果【2】》链接地址:http://blog.csdn.net/zhangphil/article/details/50154707
【3】Android谷歌官方Settings开发文档:http://developer.android.com/guide/topics/ui/settings.html