当前位置:编程学习 > XML/UML >>

Android ApiDemos示例解析(30):App->Preferences->Preferences from XML

我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状态,Shared Preferences更一般的用法是用来存储一些应用程序偏好(设置)。 包android.preference 提供了很多类可以方便应用程序来显示和设置应用相关的偏好。当然你可以使用自定义的UI来配置这些程序偏好。但使用android.preference中定义的类可以给用户一个统一的UI (和Android本身的Settings一致)。
通常情况下,程序偏好使用单独的Activity(派生于PreferenceActivity)来完成。在PreferenceActivity中,PreferenceScreen为Layout 的root element ,它可以包含其它如:CheckBoxPreference ,EditTextPreference, ListPreference ,PreferenceCategory或RingtonPreference. 使用Preference时所有程序偏好将会自动保存在应用程序的SharedPreferences中, 应用可以通过getSharedPreferences() 来访问这些偏好设置。
• CheckBoxPreference 使用Checkbox 来显示某个配置项。
• EditTextPreference 使用文本框来显示某个牌子项,允许接受用户输入文本。
• ListPreference 使用一组单选钮 (列表)可以从中选择某一项。
• MultiSelectListPreference 使用一组Checkbox,允许该配置项有多值。
• RingtonPreference 允许用户从选取某个铃声
这些类的基类为Preference ,以它为基类,也可以定义自定义的Preference.
• PreferenceGroup 可以为多个Preference定义一个组,PreferenceCategory, PreferenceScreen为它的子类。
• PreferenceCategory 同样可以包含多个Preferneces ,如果该组被Disable时,可以提供一个标题。
• PreferenceScreen 为 Preferences层次结构的根元素,PreferenceScreen可以实现嵌套。内层的PreferenceScreen将会使用一个新的屏幕显示,有点类似于Word中的分页功能。
Preferences from XML 介绍了使用XML来定义应用程序偏好,并使用PreferenceActivity来显示这个偏好。
R.xml.preferences的定义如下:
<PreferenceScreen
xmlns:android=”http://schemas.android.com/apk/res/android”>
<PreferenceCategory
android:title=”@string/inline_preferences”>
<CheckBoxPreference
android:key=”checkbox_preference”
android:title=”@string/title_toggle_preference”
android:summary=”@string/summary_toggle_preference” />
</PreferenceCategory>
<PreferenceCategory
android:title=”@string/dialog_based_preferences”>
<EditTextPreference
android:key=”edittext_preference”
android:title=”@string/title_edittext_preference”
android:summary=”@string/summary_edittext_preference”
android:dialogTitle=”@string/dialog_title_edittext_preference” />
<ListPreference
android:key=”list_preference”
android:title=”@string/title_list_preference”
android:summary=”@string/summary_list_preference”
android:entries=”@array/entries_list_preference”
android:entryValues=”@array/entryvalues_list_preference”
android:dialogTitle=”@string/dialog_title_list_preference” />
</PreferenceCategory>
<PreferenceCategory
android:title=”@string/launch_preferences”>
<!– This PreferenceScreen tag serves as a screen break (similar to page break
in word processing). Like for other preference types, we assign a key
here so it is able to save and restore its instance state. –>
<PreferenceScreen
android:key=”screen_preference”
android:title=”@string/title_screen_preference”
android:summary=”@string/summary_screen_preference”>
<!– You can place more preferences here that will be shown on the next screen. –>
<CheckBoxPreference
android:key=”next_screen_checkbox_preference”
android:title=”@string/title_next_screen_toggle_preference”
android:summary=”@string/summary_next_screen_toggle_preference” />
</PreferenceScreen>
<PreferenceScreen
android:title=”@string/title_intent_preference”
android:summary=”@string/summary_intent_preference”>
<intent android:action=”android.intent.action.VIEW”
android:data=”http://www.android.com” />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:title=”@string/preference_attributes”>
<CheckBoxPreference
android:key=”parent_checkbox_preference”
android:title=”@string/title_parent_preference”
android:summary=”@string/summary_parent_preference” />
<!– The visual style of a child is defined by this styled theme attribute. –>
<CheckBoxPreference
android:key=”child_checkbox_preference”
android:dependency=”parent_checkbox_preference”
android:layout=”?android:attr/preferenceLayoutChild”
android:title=”@string/title_child_preference”
android:summary=”@string/summary_child_preference” />
</PreferenceCategory>
</PreferenceScreen>
 
而在代码中使用addPreferencesFromResource(R.xml.preferences) 显示出XML所定义的Preferences。
XML中根元素为PreferenceScreen.
CheckBoxPreference
PreferenceCategory定义该组配置的标题,CheckBoxPreference使用Checkbox来显示该配置项。
<PreferenceCategory
android:title=”@string/inline_preferences”>
<CheckBoxPreference
android:key=”checkbox_preference”
android:title=”@string/title_toggle_preference”
android:summary=”@string/summary_toggle_preference” />
</PreferenceCategory>

 

EditTextPreference
EditTextPrefernece显示一个文本框来接受用户输入:
<EditTextPreference
android:key=”edittext_preference”
android:title=”@string/title_edittext_preference”
android:summary=”@string/summary_edittext_preference”
android:dialogTitle=”@string/dialog_title_edittext_preference” />
 

ListPreference
显示一组单选钮。
<ListPreference
android:key=”list_preference”
android:title=”@string/title_list_preference”
android:summary=”@string/summary_list_preference”
android:entries=”@array/entries_list_preference”
android:entryValues=”@array/entryvalues_list_preference”
android:dialogTitle=”@string/dialog_title_list_preference” />

 

PreferenceScreen
使用新的屏幕显示该应用程序偏好配置。
<PreferenceScreen
android:key=”screen_preference”
android:title=”@string/title_screen_preference”
android:summary=”@string/summary_screen_preference”>
<!– You can place more preferences here that will be shown on the next screen. –>
<CheckBoxPreference
android:key=”next_screen_checkbox_preference”
android:title=”@string/title_next_screen_toggle_preference”
android:summary=”@string/summary_next_screen_toggle_preference” />
</PreferenceScreen>

 

除了新起一个屏幕之外,PreferenceScreen也可以用来启动一个Activity,下面定义启动浏览器打开http://www.android.com。
<PreferenceScreen
android:title=”@str
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,