如果没有特殊要求,我们可以使用android提供的框架来创建系统样式的preference screen,在其内部可以包含preferencecategory和preference<control>元素的任意组合。建立preference screen,需要向里面添加首选项控件,原生的这些控件有:checkboxpreference、edittextpreference、listpreference、multiselectlistpreference和ringtonepreference,每种控件至少包含4种属性:android:key,android:title,android:summary,android:defaultvalue,用一种图来对其进行说明:
创建preference有两种方法,第一种是使用preferencesactivity。首先在res/values下创建一个arrays.xml用于存放listpreference的数据
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="update_freq_options">
<item>every minute</item>
<item>5 minutes</item>
<item>10 minutes</item>
<item>15 minutes</item>
<item>every hour</item>
</string-array>
<string-array name="magnitude">
<item>1</item>
<item>3</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
</string-array>
<string-array name="magnitude_options">
<item>1</item>
<item>3</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
</string-array>
<string-array name="update_freq_values">
<item>1</item>
<item>5</item>
<item>10</item>
<item>15</item>
<item>60</item>
</string-array>
</resources>
然后在res/xml下创建userpreferences.xml,用于定义preferencescreen
<?xml version="1.0" encoding="utf-8"?>
<preferencescreen
xmlns:android="http://schemas.android.com/apk/res/android">
<checkboxpreference
android:key="pref_antuo_update"
android:title="auto refresh"
android:summary="select to turn on automatic updating"
android:defaultvalue="true"/>
<listpreference
android:key="pref_update_freq"
android:title="prefresh frequency"
android:summary="frequency at which to refresh earthquake list"
android:entries="@array/update_freq_options"
android:entryvalues="@array/update_freq_values"
android:dialogtitle="prefresh frequency"
android:defaultvalue="60"/>
<listpreference
android:key="pref_min_mag"
android:title="minmum magnitude"
android:summary="select the minimum magnitude earthquake to report"
android:entries="@array/magnitude_options"
android:entryvalues="@array/magnitude"
android:dialogtitle="magnitude"
android:defaultvalue="3"/>
</preferencescreen>
最后在mainactivity中重写oncreate方法,这个mainactivity要继承preferenceactivity,使用addpreferencesfromresource添加preferencescreen
public class mainactivity extends preferenceactivity
{
@override
protected void oncreate(bundle savedinstancestate)
{
// todo auto-generated method stub
super.oncreate(savedinstancestate);
addpreferencesfromresource(r.xml.userpreferences);
}
}
ok,搞定,效果如图
第二种方法是使用preferencefragment,首先创建usepreferencefragment类继承preferencefragment类,重写oncreate方法,使用addpreferencesfromresource来加载userpreferences.xml
public class userpreferencefragment extends preferencefragment
{
@override
public void oncreate(bundle savedinstancestate)
{
// todo auto-generated method stub
super.oncreate(savedinstancestate);
addpreferencesfromresource(r.xml.userpreferences);
}
}
然后在res/xml下创建preference_headers.xml,来关联usepreferencefragment
<?xml version="1.0" encoding="utf-8"?>
<preference-headers
xmlns:android="http://schemas.android.com/apk/res/android">
<header android:fragment="com.sharedpreferencetest.userpreferencefragment"
android:title="settings"
android:summary="earthquake refresh settings" />
</preference-headers>
最后在mainactivity中重写onbuildheaders方法
public class mainactivity extends preferenceactivity
{
@override
public void onbuildheaders(list<header> target)
{
// todo auto-generated method stub
loadheadersfromresource(r.xml.preference_headers, target);
}
}
搞定,效果如图: 然后点击settings就会进入下个界面