PreferenceActivity 的用法总结

第一步:

定义Activity 继承 PreferenceActivity 

使用addPreferencesFromResource(R.xml.preferences); 显示 xml

第二步:

编写preferences xml文件保存再 res/xml目录下

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <CheckBoxPreference
        android:defaultValue="false"
        android:key="running_state"
        android:summary="@string/running_summary_stopped"      //摘要
        android:title="@string/running_label" />                 //标题

    <PreferenceCategory                             //分类、种类
        android:key="settings"
        android:title="@string/settings_label" >
        <EditTextPreference
            android:defaultValue="@string/username_default"
            android:key="username"
            android:title="@string/username_label" />       
        
        <PreferenceScreen                                //点击启动一个新的Preference 界面,如果配置intent节点,甚至可以启动浏览器打开web
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:title="@string/advancedsettings_label" >
            <EditTextPreference
                android:defaultValue="@string/portnumber_default"
                android:key="portNum"
                android:title="@string/portnumber_label" />
           <CheckBoxPreference
                android:defaultValue="@string/wakelock_default"
                android:key="stayAwake"
                android:title="@string/wakelock_label" />
        </PreferenceScreen>
        
    </PreferenceCategory>
    
    <PreferenceCategory
        android:key="extra_category"
        android:title="@string/extra_label" >
        <Preference
            android:key="help"
            android:title="@string/help_label" />        
    </PreferenceCategory>

</PreferenceScreen>

第三步:

再code 中添加监听和初始化

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);//==getSharedPreferences("packageName_preferences" ,MODE_PRIVATE);
        Resources resources = getResources();

        CheckBoxPreference running_state = (CheckBoxPreference) findPreference("running_state");//保存再shared_prefs文件夹下的xml文件中,其中key为键,value为值保存再map节点下。
        running_state.setChecked(FTPServerService.isRunning());
        running_state.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
        {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue)
            {
                if ((Boolean) newValue)
                {
                }                return true;
            }
        });

        EditTextPreference username_pref = (EditTextPreference) findPreference("username");
        username_pref.setSummary(settings.getString("username", resources.getString(R.string.username_default)));
        username_pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
        {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue)
            {
                String newUsername = (String) newValue;
                if (preference.getSummary().equals(newUsername))
                    return false;
                if (!newUsername.matches("[a-zA-Z0-9]+"))//采用正则表达式匹配用户名
                {
                    Toast.makeText(ServerPreferenceActivity.this, R.string.username_validation_error, Toast.LENGTH_LONG).show();
                    return false;
                }
                preference.setSummary(newUsername);
                stopServer();
                return true;
            }
        });
    
CheckBoxPreference acceptproxy_pref = (CheckBoxPreference) findPreference("allowNet"); acceptwifi_pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if ((Boolean) newValue == false) acceptproxy_pref.setChecked(true); stopServer(); return true; } });

       help = findPreference("help"); help.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { new AlertDialog.Builder(ServerPreferenceActivity.this).setTitle(R.string.help_dlg_title).setMessage(R.string.help_dlg_message) .setPositiveButton(getText(R.string.ok), null).show(); return true; } });
posted @ 2012-07-30 11:25  lipeil  阅读(3227)  评论(0编辑  收藏  举报