运行结果:
[img]http://dl.iteye.com/upload/attachment/536602/2768b06d-283b-31a6-b5a9-d2fcb399d74d.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/536604/0bc1174a-81a4-3a85-9c90-57748005fe8c.jpg[/img]
main.xml:
array.xml:
activity.java
以上是preferenceactivity的xml描述,那么在程序中我们只需要新建一个继承自preferenceactivity的activity,然后在主程序中调用就可以了。这个preferenceactivity中的设置存储是完全自动的,你不需要再用代码去实现设置的存储,preferenceactivity创建后会自动创建一个配置文件/data/data/you_package_name/shared_prefs/you_package_name_you_xml_name.xml。上例中自动生成的配置文件如下:
要取得其中的值可以通过如下的方法:
[img]http://dl.iteye.com/upload/attachment/536602/2768b06d-283b-31a6-b5a9-d2fcb399d74d.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/536604/0bc1174a-81a4-3a85-9c90-57748005fe8c.jpg[/img]
main.xml:
<?xml version="1.0" encoding="utf-8"?><preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preferencecategory android:title="preferencecategory 1"> <checkboxpreference android:key="checkbox1" android:title="checkbox" android:summaryon="某功能: 开启" android:summaryoff="某功能: 关闭" android:defaultvalue="true" /> </preferencecategory> <preferencecategory android:title="preferencecategory 2"> <preferencescreen android:title="二级preferencescreen"> <checkboxpreference android:key="checkbox2" android:title="checkbox" android:summaryon="某功能: 开启" android:summaryoff="某功能: 关闭" android:defaultvalue="true" /> </preferencescreen> </preferencecategory> <preferencecategory android:title="preferencecategory 3"> <listpreference android:key="listpreference" android:title="listpreference" android:summary="listpreference测试" android:dialogtitle="listpreference" android:entries="@array/entries_list_preference" android:entryvalues="@array/entriesvalue_list_preference" /> <edittextpreference android:key="edittextpreference" android:title="edittextpreference" android:summary="点击输入" android:dialogtitle="输入设置" /> <ringtonepreference android:key="ringtonepreference" android:title="ringtonepreference" android:summary="选择铃声" /> </preferencecategory></preferencescreen>
array.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="entries_list_preference"> <item>test1</item> <item>test2</item> <item>test3</item> </string-array> <string-array name="entriesvalue_list_preference"> <item>1</item> <item>2</item> <item>3</item> </string-array></resources>
activity.java
@overridepublic void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.layout.main);}
以上是preferenceactivity的xml描述,那么在程序中我们只需要新建一个继承自preferenceactivity的activity,然后在主程序中调用就可以了。这个preferenceactivity中的设置存储是完全自动的,你不需要再用代码去实现设置的存储,preferenceactivity创建后会自动创建一个配置文件/data/data/you_package_name/shared_prefs/you_package_name_you_xml_name.xml。上例中自动生成的配置文件如下:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map><string name="edittextpreference">12332312</string><string name="listpreference">2</string><string name="ringtonepreference">content://settings/system/ringtone</string><boolean name="checkbox1" value="true" /><boolean name="checkbox2" value="true" /></map>
要取得其中的值可以通过如下的方法:
sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(this);value = prefs.getstring("listpreference", "unset");