MainActivity如下:
package cn.testpreferenceactivity; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; /** * Demo描述: * PreferenceActivity的使用示例 * * 注意事项: * 1 不要ListPreference的属性android:dialogMessage * 若设置则无法弹出选择列表. * 这和单选,多选对话框类似不能设置其Message. * 2 不要ListPreference的属性android:positiveButtonText * 即使设置也无效 * 3 注意ListPreference的默认值的设定 * android:defaultValue="222" * 该值必须是 android:entryValues="@array/values_array" * 这个数组中包含的值!!!!!!!!!!!!! * * * 参考资料: * 1 http://www.cnblogs.com/adm1989/archive/2012/08/01/2618403.html * 2 http://www.eoeandroid.com/forum.php?mod=viewthread&tid=188859 * Thank you very much */ public class MainActivity extends PreferenceActivity { private CheckBoxPreference mCheckBoxPreference; private ListPreference mListPreference; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferenceactivity); init(); } private void init(){ mCheckBoxPreference= (CheckBoxPreference) findPreference(getResources().getString(R.string.first_key)); mCheckBoxPreference.setOnPreferenceChangeListener(new PreferenceChangeListenerImpl()); mCheckBoxPreference.setOnPreferenceClickListener(new PreferenceClickListenerImpl()); mListPreference= (ListPreference) findPreference(getResources().getString(R.string.listpreference_key)); mListPreference.setOnPreferenceChangeListener(new PreferenceChangeListenerImpl()); mListPreference.setOnPreferenceClickListener(new PreferenceClickListenerImpl()); } private class PreferenceClickListenerImpl implements OnPreferenceClickListener{ @Override public boolean onPreferenceClick(Preference preference) { String preferenceKey=preference.getKey().toString(); if (preferenceKey.equals(getResources().getString(R.string.first_key))) { System.out.println("点击了CheckBoxPreference"); } if (preferenceKey.equals(getResources().getString(R.string.listpreference_key))) { System.out.println("点击了ListPreference"); } return true; } } /** * 注意事项: * 在OnPreferenceChangeListener的onPreferenceChange() * 可知改变后的值,但该值还未真正存入尚在内存中贮存. * 所以此时调用getValuesFromSP得到的只是改变前的值. * */ private class PreferenceChangeListenerImpl implements OnPreferenceChangeListener{ @Override public boolean onPreferenceChange(Preference preference, Object newValue) { String preferenceKey=preference.getKey().toString(); if (preferenceKey.equals(getResources().getString(R.string.first_key))) { System.out.println("切换后值为:"+newValue.toString()); } if (preferenceKey.equals(getResources().getString(R.string.listpreference_key))) { System.out.println("切换后值为:"+newValue.toString()); } return true; } } @Override public void onBackPressed() { getValuesFromSP(); super.onBackPressed(); } // 从PreferenceActivity的SharedPreferences中取数据 private void getValuesFromSP(){ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); Boolean first_value = sp.getBoolean(getResources().getString(R.string.first_key), false); String listpreference_key = sp.getString(getResources().getString(R.string.listpreference_key), null); System.out.println("从SharedPreferences中取出的是:" + first_value); System.out.println("从SharedPreferences中取出的是:" + listpreference_key); } }
preferenceactivity.xml如下:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <CheckBoxPreference android:key="@string/first_key" android:summary="@string/first_default" android:summaryOff="@string/first_off" android:summaryOn="@string/first_on" android:title="@string/first_title" /> <ListPreference android:dialogIcon="@drawable/ic_launcher" android:dialogTitle="@string/listpreference_title" android:entries="@array/names_array" android:entryValues="@array/values_array" android:key="@string/listpreference_key" android:negativeButtonText="@string/listpreference_no" android:summary="@string/listpreference_summary" android:title="@string/listpreference_title" android:defaultValue="222" /> </PreferenceScreen>
strings.xml如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TestPreferenceActivity</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="first_key">first_key</string> <string name="first_on">first_on</string> <string name="first_off">first_off</string> <string name="first_title">first_title</string> <string name="first_default">first_default</string> <string name="listpreference_key">listpreference_key</string> <string name="listpreference_title">listpreference_title</string> <string name="listpreference_summary">listpreference_summary</string> <string name="listpreference_message">listpreference_message</string> <string name="listpreference_ok">OK</string> <string name="listpreference_no">NO</string> </resources>
时间: 2024-10-27 17:51:10