在开发应用程序的过程中我们有很大的机会需要用到参数设置功能,那么在Android应用中,我们如何实现参数设置界面及参数存储呢,下面我们来介绍一下Android中的一个特殊Activity–PreferencesActivity。PreferencesActivity是Android中专门用来实现程序设置界面及参数存储的一个Activity,我们用一个实例来简介如何使用PreferencesActivity。
通过查看SDK自带的ApiDemo可以发现,里面有一个类叫PreferenceActivity,里面的第一个例子是通过xml文件来实现的,其它的还没研究(首先通过Eclipse把ApiDemo安装到手机上,然后从手机上打开,选择第一个App,大概在第二页的样子就有一个Preferences,点进去就有了),稍微整理了一下,发出来。。。
按照惯例,还是先发图看看效果,如果不是你们所要的效果,也不浪费你们的时间
效果图就是这样了,现在就开始码代码吧。。。
注意这个界面并非传统的layout布局格式!!!
在res目录下面新建一个文件夹xml,然后在里面新建文件preferences.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="@string/inline_preferences" > <CheckBoxPreference android:key="checkbox_preference" android:summary="@string/summary_toggle_preference" android:title="@string/title_toggle_preference" /> </PreferenceCategory> <PreferenceCategory android:title="@string/dialog_based_preferences" > <EditTextPreference android:dialogTitle="@string/dialog_title_edittext_preference" android:key="edittext_preference" android:summary="@string/summary_edittext_preference" android:title="@string/title_edittext_preference" /> <ListPreference android:dialogTitle="@string/dialog_title_list_preference" android:entries="@array/entries_list_preference" android:entryValues="@array/entryvalues_list_preference" android:key="list_preference" android:summary="@string/summary_list_preference" android:title="@string/title_list_preference" /> </PreferenceCategory> <PreferenceCategory android:title="@string/launch_preferences" > <PreferenceScreen android:key="screen_preference" android:summary="@string/summary_screen_preference" android:title="@string/title_screen_preference" > <CheckBoxPreference android:key="next_screen_checkbox_preference" android:summary="@string/summary_next_screen_toggle_preference" android:title="@string/title_next_screen_toggle_preference" /> </PreferenceScreen> <PreferenceScreen android:summary="@string/summary_intent_preference" android:title="@string/title_intent_preference" > <intent android:action="android.intent.action.VIEW" android:data="http://www.baidu.com" /> </PreferenceScreen> </PreferenceCategory> <PreferenceCategory android:title="@string/preference_attributes" > <CheckBoxPreference android:key="parent_checkbox_preference" android:summary="@string/summary_parent_preference" android:title="@string/title_parent_preference" /> <CheckBoxPreference android:dependency="parent_checkbox_preference" android:key="child_checkbox_preference" android:layout="?android:attr/preferenceLayoutChild" android:summary="@string/summary_child_preference" android:title="@string/title_child_preference" /> </PreferenceCategory> </PreferenceScreen>
当然里面用到了一些字符串,定义在Strings.xml文件里面的
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">Preferences_Demo</string> <string name="inline_preferences">In-line preferences</string> <string name="title_toggle_preference">Toggle preference</string> <string name="summary_toggle_preference">This is a toggle button</string> <string name="dialog_based_preferences">Dialog-based preferences</string> <string name="title_edittext_preference">Edit text preference</string> <string name="dialog_title_edittext_preference">Enter your favorite animal</string> <string name="summary_edittext_preference">An example that uses an edit text dialog</string> <string name="title_list_preference">List preference</string> <string name="summary_list_preference">An example that uses a list dialog</string> <string name="preference_attributes">Preference attributes</string> <string name="title_parent_preference">Parent toggle</string> <string name="summary_parent_preference">This is visually a parent</string> <string name="title_child_preference">Child toggle</string> <string name="summary_child_preference">This is visually a child</string> <string name="title_next_screen_toggle_preference">Toggle preference</string> <string name="summary_next_screen_toggle_preference">Preference that is on the next screen but same hierarchy</string> <string name="title_intent_preference">Intent preference</string> <string name="summary_intent_preference">Launches an Activity from an Intent</string> <string name="title_screen_preference">Screen preference</string> <string name="summary_screen_preference">Shows another screen of preferences</string> <string name="dialog_title_list_preference">Choose one</string> <string name="launch_preferences">Launch preferences</string> </resources>
还有两个array数组,在values文件夹下面建一个arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="entries_list_preference"> <item>Alpha Option 01</item> <item>Beta Option 02</item> <item>Charlie Option 03</item> </string-array> <string-array name="entryvalues_list_preference"> <item>alpha</item> <item>beta</item> <item>charlie</item> </string-array> </resources>
OK,下面写主类MainActivity,它继承自PreferenceActivity
package com.and.preferences; import android.os.Bundle; import android.preference.PreferenceActivity; public class MainActivity extends PreferenceActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } }
行了,ApiDemo里面好像还提供几种,有待于研究。。。。