在android开发应用中我们会用到PreferenceActivity这个参数设置功能类。下面我们来介绍一下Android中的一个特殊Activity–PreferencesActivity。
PreferenceActivity是android中设置和设置程序设置界面和简单参数存储的Activity。
例子如下:
建立一个xml文件来描述这个界面:
<?xml version="1.0" encoding="utf-8" ?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/game_preferences_category">
<CheckBoxPreference android:title="@string/music_title" android:key="music_key" android:defaultValue="true" android:summary="@string/music_summary"></CheckBoxPreference>
<CheckBoxPreference android:title="@string/sounds_title" android:key="sounds_key" android:defaultValue="true" android:summary="@string/sounds_summary"></CheckBoxPreference>
<CheckBoxPreference android:title="@string/vibration_title" android:key="vibration_key" android:defaultValue="true" android:summary="@string/vibration_summary"></CheckBoxPreference>
<ListPreference android:title="@string/background_title" android:key="background_key" android:summary="@string/background_summary" android:entries="@array/background_entries" android:entryValues="@array/background_Value"></ListPreference>
<ListPreference android:title="@string/song_title" android:key="song_key" android:summary="@string/song_summary" android:entries="@array/song_enties" android:entryValues="@array/song_Value"></ListPreference>
</PreferenceCategory>
</PreferenceScreen>
上面的xml文件包含了PreferenceActivity的一些常用控件:
PreferenceScreen是设置界面的相关控件,可嵌套成二级设置页面。
PreferenceCategoty 某一类相关的设置可用Title设置相关标题。
CheckBoxPreference 是CheckBox相关的设置可用Title设置相关参数,有两种值,true或false。
summaryOn 和summaryOff用来设置控件选择和未选择的状态。
ListPrefence:下来框选择控件,用Title来设置相关标题,用summary来设置相关下拉说明,用android:entries设置下拉选中项的名称,android:entryValues来设置下来选项的标号。
其具体内容和下拉说明需在res/values/array.xml中显示:
<?xml
version="1.0" encoding="utf-8" ?>
<resources>
<string-array name="background_entries">
<item>Field (Night)</item>
<item>Field (Day)</item>
</string-array>
<string-array name="background_Value">
<item>001</item>
<item>002</item>
</string-array>
<string-array name="song_enties">
<item>Song 1</item>
<item>Song 2</item>
<item>Song 3</item>
<item>Song 4</item>
</string-array>
<string-array name="song_Value">
<item>001</item>
<item>002</item>
<item>003</item>
<item>004</item>
</string-array>
</resources>