1.PreferenceActivity主要应用与对用户的偏好设置,通过它的命名也可以看出来这点!
我们都知道Android持久化有四种:SQLite、文件存储、Preference、ContentProvider
而其中Preference是以类似Map的键值对形式存储的,最适合用来保存用户个人设置之类的信息
2.怎么来使用呢,只要一个类继承它,并添加一个xml文件来创建它的页面就可以了!
a.首先在res目录下建立一个xml文件夹,然后新建一个preferences.xml文件
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/steps_settings_title"> <ListPreference android:key="sensitivity" android:title="@string/sensitivity_setting" android:summary="@string/sensitivity_setting_details" android:entries="@array/sensitivity_preference" android:entryValues="@array/sensitivity_preference_values" android:dialogTitle="@string/sensitivity_setting_title" android:defaultValue="30" /> <ListPreference android:key="operation_level" android:title="@string/operation_level_setting" android:summary="@string/operation_level_setting_details" android:entries="@array/operation_level_preference" android:entryValues="@array/operation_level_preference_values" android:dialogTitle="@string/operation_level_setting_title" android:defaultValue="30" /> <ListPreference android:key="units" android:title="@string/units_setting" android:summary="@string/units_setting_details" android:entries="@array/units_preference" android:entryValues="@array/units_preference_values" android:dialogTitle="@string/units_setting_title" android:defaultValue="imperial" /> <cq.cake.pedometer.perferences.StepLengthPreference android:key="step_length" android:title="@string/step_length_setting" android:summary="@string/step_length_setting_details" android:dialogTitle="@string/step_length_setting_title" android:defaultValue="20" /> <cq.cake.pedometer.perferences.BodyWeightPreference android:key="body_weight" android:title="@string/body_weight_setting" android:summary="@string/body_weight_setting_details" android:dialogTitle="@string/body_weight_setting_title" android:defaultValue="50" /> <ListPreference android:key="exercise_type" android:title="@string/exercise_type_setting" android:summary="@string/exercise_type_setting_details" android:entries="@array/exercise_type_preference" android:entryValues="@array/exercise_type_preference_values" android:dialogTitle="@string/exercise_type_setting_title" android:defaultValue="running" /> <ListPreference android:key="maintain" android:title="@string/maintain_setting" android:summary="@string/maintain_setting_details" android:entries="@array/maintain_preference" android:entryValues="@array/maintain_preference_values" android:dialogTitle="@string/maintain_setting_title" android:defaultValue="none" /> </PreferenceCategory> <PreferenceCategory android:title="@string/voice_settings_title"> <CheckBoxPreference android:key="speak" android:title="@string/voice_setting" android:summary="@string/voice_setting_details" android:defaultValue="false" /> <ListPreference android:key="speaking_interval" android:title="@string/speaking_interval_setting" android:summary="@string/speaking_interval_setting_details" android:entries="@array/speaking_interval_preference" android:entryValues="@array/speaking_interval_preference_values" android:dependency="speak" android:defaultValue="1" /> <PreferenceScreen android:key="tell_what" android:title="@string/tell_what" android:dependency="speak"> <PreferenceCategory android:title="@string/tell_what"> <CheckBoxPreference android:key="tell_steps" android:title="@string/tell_steps_setting" android:summary="@string/tell_steps_setting_details" android:defaultValue="false" /> <CheckBoxPreference android:key="tell_pace" android:title="@string/tell_pace_setting" android:summary="@string/tell_pace_setting_details" android:defaultValue="false" /> <CheckBoxPreference android:key="tell_distance" android:title="@string/tell_distance_setting" android:summary="@string/tell_distance_setting_details" android:defaultValue="false" /> <CheckBoxPreference android:key="tell_speed" android:title="@string/tell_speed_setting" android:summary="@string/tell_speed_setting_details" android:defaultValue="false" /> <CheckBoxPreference android:key="tell_calories" android:title="@string/tell_calories_setting" android:summary="@string/tell_calories_setting_details" android:defaultValue="false" /> <CheckBoxPreference android:key="tell_fasterslower" android:title="@string/tell_fasterslower_setting" android:summary="@string/tell_fasterslower_setting_details" android:defaultValue="false" /> </PreferenceCategory> </PreferenceScreen> </PreferenceCategory> </PreferenceScreen>
b.然后创建一个Settings类来继承PreferenceActivity,在onCreate里面添加这个xml
package cq.cake.pedometer; import android.os.Bundle; import android.preference.PreferenceActivity; public class Settings extends PreferenceActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } }
a-1 其中ListPreference是一个让用户选择列表的形式出现控件,其中key代表键值,用于其它地方需要这个值得时候,通过PreferenceManager.getDefaultSharedPreferences(Context)的静态方法,获取到一个全局的Preference对象,再通过这个对象的get相关方法取值,例如:
SharedPreferences setPreference = PreferenceManager.getDefaultSharedPreferences(this); setPreference.getString("sensitivity","中等");//这里默认就是取的【中等】值,如果没有的话例如这张图片,可以选择极低,但如果是第一次没有选择,则默认使用中等这个值
还有的属性:title:标题 summary:描述 entries:显示给用户的数组值,我们可以保存在values下面的arrays.xml中,entryValues就是实际的数组值,dialogTitle表示弹出来的标题,defaultValue就是默认设置的值
<string-array name="sensitivity_preference"> <item>@string/extra_high</item> <item>@string/very_high</item> <item>@string/high</item> <item>@string/higher</item> <item>@string/medium</item> <item>@string/lower</item> <item>@string/low</item> <item>@string/very_low</item> <item>@string/extra_low</item> </string-array> <string-array name="sensitivity_preference_values"> <item>1.9753</item> <item>2.9630</item> <item>4.4444</item><!-- 10 * 1.5^(-2) --> <item>6.6667</item><!-- 10 * 1.5^(-1) --> <item>10</item> <item>15</item><!-- 10 * 1.5 --> <item>22.5</item><!-- 10 * 1.5^2 --> <item>33.75</item> <item>50.625</item> </string-array>
a-2 EditTextPreference,为文本编辑框配置控件,上面的preferences.xml中的StepLengthPreference自定义类就是继承了它,里面的属性和第一个控件意思一样
a-3 CheckBoxPreference,就是单选的喜好控件,其他属性一样,最后一个属性defaultValue只能输入false或true,代表了真假,和普通的CheckBox样式区别不大,只是它是Preference类型的控件而已
a-4 PreferenceScreen代表了新的喜好界面,可以在这里面嵌套它,点击又会生成新的喜好界面。
a-5 PreferenceCategory就是喜欢界面的下个分组标签,代表了一组属性值