Android学习笔记之Shared Preference
如果没有特殊要求,我们可以使用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就会进入下个界面