界面XML文件:preference_setting.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
- <PreferenceCategory android:title="我的位置源">
- <CheckBoxPreference
- android:key="my_wireless_network"
- android:title="使用无线电"
- android:summary="使用无线网络查看应用程序(例如Google地图)中的位置"
- />
- <CheckBoxPreference
- android:key="my_gps_satellite_setting"
- android:title="启用GPS卫星设置"
- android:summary="定位时,精确到街道级别(取消选择可节约电量)"
- />
- </PreferenceCategory>
- <PreferenceCategory android:title="个人信息设置">
- <CheckBoxPreference
- android:key="my_yesno_save_individual_info"
- android:title="是否保存个人信息"
- />
- <EditTextPreference
- android:key="my_individual_name"
- android:title="姓名"
- android:summary="请输入真实姓名"
- />
- <PreferenceScreen
- android:key="my_other_individual_msg"
- android:title="其他个人信息"
- android:summary="是否工作、手机">
- <CheckBoxPreference
- android:key="my_is_an_employee"
- android:title="是否工作"
- />
- <EditTextPreference
- android:key="my_mobile"
- android:title="手机"
- android:summary="请输入您的手机号码"
- />
- </PreferenceScreen>
- </PreferenceCategory>
- </PreferenceScreen>
<?xml version="1.0" encoding="UTF-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="我的位置源">
<CheckBoxPreference
android:key="my_wireless_network"
android:title="使用无线电"
android:summary="使用无线网络查看应用程序(例如Google地图)中的位置"
/>
<CheckBoxPreference
android:key="my_gps_satellite_setting"
android:title="启用GPS卫星设置"
android:summary="定位时,精确到街道级别(取消选择可节约电量)"
/>
</PreferenceCategory>
<PreferenceCategory android:title="个人信息设置">
<CheckBoxPreference
android:key="my_yesno_save_individual_info"
android:title="是否保存个人信息"
/>
<EditTextPreference
android:key="my_individual_name"
android:title="姓名"
android:summary="请输入真实姓名"
/>
<PreferenceScreen
android:key="my_other_individual_msg"
android:title="其他个人信息"
android:summary="是否工作、手机">
<CheckBoxPreference
android:key="my_is_an_employee"
android:title="是否工作"
/>
<EditTextPreference
android:key="my_mobile"
android:title="手机"
android:summary="请输入您的手机号码"
/>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
PreferenceActivity 的类文件:
- package com.icitylife.preference;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.preference.Preference;
- import android.preference.PreferenceActivity;
- import android.preference.PreferenceScreen;
- import android.preference.Preference.OnPreferenceChangeListener;
- public class Main extends PreferenceActivity implements OnPreferenceChangeListener {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //设置PreferenceActivity保存数据使用的XML文件的名称
- getPreferenceManager().setSharedPreferencesName("mySetting");
- //加载XML资源文件:此处就不能使用Activity的setContentView()
- addPreferencesFromResource(R.xml.preference_setting);
- //获取【姓名】设置项对应的Preference对象
- Preference individualNamePreference=findPreference("my_individual_name");
- //获得指向mySetting.xml文件的SharedPreference对象
- SharedPreferences sharedPreferences=individualNamePreference.getSharedPreferences();
- //设置【姓名】设置项的summary
- individualNamePreference.setSummary(sharedPreferences.getString("my_individual_name",""));
- //判断【是否保存个人信息】设置项是否被选中
- if(sharedPreferences.getBoolean("my_yesno_save_individual_info",false)){
- individualNamePreference.setEnabled(true);
- }else{
- individualNamePreference.setEnabled(false);
- }
- individualNamePreference.setOnPreferenceChangeListener(this);
- //获得【电话号码】设置项对应的Preference对象
- Preference mobileNamePreference=findPreference("my_mobile");
- SharedPreferences mobileSharedPreferences=mobileNamePreference.getSharedPreferences();
- //设置【电话号码】的summary
- mobileNamePreference.setSummary(mobileSharedPreferences.getString("my_mobile",""));
- mobileNamePreference.setOnPreferenceChangeListener(this);
- }
- @Override
- public boolean onPreferenceChange(Preference preference, Object newValue) {
- preference.setSummary(String.valueOf(newValue));
- return true;
- }
- /**
- * 处理【是否保存个人信息】的改变事件
- */
- @Override
- public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
- Preference preference){
- if("my_yesno_save_individual_info".equals(preference.getKey())){
- findPreference("my_individual_name").setEnabled(!findPreference("my_individual_name").isEnabled());
- }
- return super.onPreferenceTreeClick(preferenceScreen, preference);
- }
- }