2011.07.11(4)——— android Preferences xml配置
参考:app/preferences
[url]http://www.imobilebbs.com/wordpress/?p=1194[/url]
1、preferences基本用法
容器:
组件:
2、xml定义
CheckBoxPreference
PreferenceCategory定义该组配置的标题,CheckBoxPreference使用Checkbox来显示该配置项。
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510002.png[/img]
EditTextPreference
EditTextPrefernece显示一个文本框来接受用户输入:
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510003.png[/img]
ListPreference
显示一组单选钮。
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510004.png[/img]
PreferenceScreen
使用新的屏幕显示该应用程序偏好配置。
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510005.png[/img]
除了新起一个屏幕之外,PreferenceScreen也可以用来启动一个Activity,下面定义启动浏览器打开http://www.android.com。
最后一个例子表示可以定义Preference之间的依赖关系。子Preferences只有在父Preference选中时才被Enable。
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510006.png[/img]
3、java代码:
几个特别的属性:
1、android:dependency=key
从属于谁 后面跟的为android:key的值
只有在Parent preference选中时,子Preference 才可以配置,否则子Preference 被Disabled,显示为灰色。
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510009.png[/img]
2、android:defaultValue=“”
在XML中定义Preference的缺省值。
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510010.png[/img]
注意:
CheckBoxPreference:[color=red]通过true或者false来设置初始值[/color]
EditTextPreference:[color=red]通过字符串来设置初始值[/color]
ListPreference:[color=red]通过value的值 也就是android:entryValues来设置初始值[/color]
参考:app/preferences
[url]http://www.imobilebbs.com/wordpress/?p=1194[/url]
1、preferences基本用法
容器:
PreferenceGroup 可以为多个Preference定义一个组,PreferenceCategory, PreferenceScreen为它的子类。
PreferenceCategory 同样可以包含多个Preferneces ,如果该组被Disable时,可以提供一个标题。
PreferenceScreen 为 Preferences层次结构的根元素,PreferenceScreen可以实现嵌套。内层的PreferenceScreen将会使用一个新的屏幕显示,有点类似于Word中的分页功能。
组件:
CheckBoxPreference 使用Checkbox 来显示某个配置项。
EditTextPreference 使用文本框来显示某个牌子项,允许接受用户输入文本。
ListPreference 使用一组单选钮 (列表)可以从中选择某一项。
MultiSelectListPreference 使用一组Checkbox,允许该配置项有多值。
RingtonPreference 允许用户从选取某个铃声
2、xml定义
CheckBoxPreference
PreferenceCategory定义该组配置的标题,CheckBoxPreference使用Checkbox来显示该配置项。
<PreferenceCategory
android:title=”@string/inline_preferences”>
<CheckBoxPreference
android:key=”checkbox_preference”<!--相当于id-->
android:title=”@string/title_toggle_preference”<!--标题-->
android:summary=”@string/summary_toggle_preference” /><!--描述-->
</PreferenceCategory>
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510002.png[/img]
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” /><!--弹出框的标题-->
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510003.png[/img]
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” />
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510004.png[/img]
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>
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510005.png[/img]
除了新起一个屏幕之外,PreferenceScreen也可以用来启动一个Activity,下面定义启动浏览器打开http://www.android.com。
<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>
最后一个例子表示可以定义Preference之间的依赖关系。子Preferences只有在父Preference选中时才被Enable。
<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>
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510006.png[/img]
3、java代码:
package com.example.android.apis.app;
import com.example.android.apis.R;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
public class PreferencesFromXml extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);//显示出XML所定义的Preferences
}
}
几个特别的属性:
1、android:dependency=key
从属于谁 后面跟的为android:key的值
只有在Parent preference选中时,子Preference 才可以配置,否则子Preference 被Disabled,显示为灰色。
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510009.png[/img]
2、android:defaultValue=“”
在XML中定义Preference的缺省值。
[img]http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/05/20110510010.png[/img]
注意:
CheckBoxPreference:[color=red]通过true或者false来设置初始值[/color]
android:defaultValue=”true”
EditTextPreference:[color=red]通过字符串来设置初始值[/color]
android:defaultValue=”@string/default_value_edittext_preference”
ListPreference:[color=red]通过value的值 也就是android:entryValues来设置初始值[/color]
android:defaultValue=”@string/default_value_list_preference”