android-基础编程-Preference
由于SDK封装和提供了一套基于Preference的类,使用Preference通过编辑xml配置文件,只要很少的代码就可以实现了,而且Preference本身已经实现了参数保存,不需要我们再考虑将参数保存文件.类型主要有有两类:一类是管理布局的有PreferenceScreen和PreferenceCategory;另一类是具体的设置元素,有CheckBoxPreference、ListPreference、EditTextPreference和RingtonePreference等.
具体使用:
一。1.生成一个Preference资源文件。
比如:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBoxPreference android:defaultValue="true" android:summaryOff="@string/auto_search_disable" android:summaryOn="@string/auto_search_enabled" android:key="@string/auto_search_enable_key" android:title="@string/auto_search_enable_title" android:disableDependentsState="false"> </CheckBoxPreference> <ListPreference android:dialogTitle="@string/search_rang_key" android:entryValues="@array/search_rang_values" android:entries="@array/search_rang_strings" android:positiveButtonText="@string/enterOk" android:negativeButtonText="@string/cancle" android:dependency="@string/auto_search_enable_key" android:title="@string/search_rang_key" android:summary="@string/search_rang_summery" android:key="@string/search_rang_key" android:defaultValue="@string/search_rang_default_value"> </ListPreference> </PreferenceScreen>
2.继承之PreferenceActivity代码。
public class ListPreferenceActi extends PreferenceActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
3.加载布局。
addPreferencesFromResource(R.xml.listpreference);
效果如下:
二。使用PreferenceCategory
1.布局:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="Settings"> <PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" android:title="Emotions" android:summary="settings about emotions"> <CheckBoxPreference android:title="Love me?" android:summaryOn="Yes,I love you!" android:summaryOff="No,I am sorry." android:defaultValue="true" android:key="@string/category_loveme_key"> </CheckBoxPreference> <CheckBoxPreference android:title="Hate me?" android:summaryOn="Yes,I hate you!" android:summaryOff="No,you are a good person." android:defaultValue="false"> </CheckBoxPreference> </PreferenceCategory> <PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" android:title="Relations" android:summary="settings about relations"> <CheckBoxPreference android:title="Family?" android:summaryOn="Yes,we are family!" android:summaryOff="No,I am sorry." android:defaultValue="true"> </CheckBoxPreference> <CheckBoxPreference android:title="Friends?" android:summaryOn="Yes,we are friends!" android:summaryOff="No,I am sorry." android:defaultValue="false"> </CheckBoxPreference> </PreferenceCategory> </PreferenceScreen>
2.调用:
addPreferencesFromResource(R.xml.preferencescategory);
3.效果:
三。使用PreferenceScreen
1.布局:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="Settings"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="Emotions" android:summary="settings about emotions"> <CheckBoxPreference android:title="Love me?" android:summaryOn="Yes,I love you!" android:summaryOff="No,I am sorry." android:defaultValue="true" android:key="@string/screen_loveme_key" android:disableDependentsState="false"> </CheckBoxPreference> <CheckBoxPreference android:title="Hate me?" android:summaryOn="Yes,I hate you!" android:summaryOff="No,you are a good person." android:defaultValue="false"> </CheckBoxPreference> </PreferenceScreen> <PreferenceScreen android:title="Relations" android:summary="settings about relations" xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBoxPreference android:title="Family?" android:summaryOn="Yes,we are family!" android:summaryOff="No,I am sorry." android:defaultValue="true"> </CheckBoxPreference> <CheckBoxPreference android:title="Friends?" android:summaryOn="Yes,we are friends!" android:summaryOff="No,I am sorry." android:defaultValue="false"> </CheckBoxPreference> </PreferenceScreen> </PreferenceScreen>
2.调用:
addPreferencesFromResource(R.xml.preferencesscreen);
3.效果: