《跟我一步一步來》———– Android的設定介面

來源:互聯網
上載者:User

 

一.“初次邂逅”
我們構建的應用程式經常會需要一個設定介面,用來設定應用程式的各個參數,就如同Android系統本身的設定介面一樣。
Android中提供了專門的方法來構建自己的設定介面


二.“先看外表”
首先建立一個項目

在工程的res目錄下添加xml檔案夾,並添加構建設定介面的xml檔案

我們先來編輯布局檔案setting_demo.xml
<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"android:key="setting_demo_preference" android:title="@string/title_setting_demo_preference"android:summary="@string/summary_setting_demo_preference"><PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android"android:key="common_settings" android:title="@string/common_settings"><CheckBoxPreference android:key="check_box"android:title="@string/title_check_box" android:summary="@string/summary_check_box"android:persistent="false" /><ListPreference android:key="string_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" /><EditTextPreference android:key="edit_text_preference"android:title="@string/title_edit_text_preference" android:summary="@string/summary_edit_text_preference"android:dialogTitle="@string/dialog_title_edit_text_preference" /></PreferenceCategory><PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android"android:key="advanced_settings" android:title="@string/advanced_settings"><VolumePreference android:key="media_volume"android:title="@string/media_volume_title" android:summary="@string/media_volume_summary"android:dialogTitle="@string/media_volume_title" android:persistent="false"android:order="3" android:streamType="music" /><SeekBarPreference android:key="seek_bar_preference"android:title="@string/seek_bar_preference_title"></SeekBarPreference></PreferenceCategory></PreferenceScreen>

其中的字串定義:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, SettingDemo!</string> <string name="app_name">SettingDemo</string> <string name="title_setting_demo_preference">Setting Demo</string> <string name="summary_setting_demo_preference">setting demo summary</string> <string name="title_check_box">Title Check Box</string> <string name="summary_check_box">summary check box</string> <string name="title_list_preference">title_list_preference</string> <string name="summary_list_preference">summary_list_preference</string> <string name="dialog_title_list_preference">dialog_title_list_preference</string> <string name="title_edit_text_preference">title_edit_text_preference</string> <string name="summary_edit_text_preference">summary_edit_text_preference</string> <string name="dialog_title_edit_text_preference">dialog_title_edit_text_preference</string> <string name="common_settings">普通設定</string> <string name="advanced_settings">進階設定</string> <string name="media_volume_title">media_volume_title</string> <string name="media_volume_summary">media_volume_summary</string> <string name="seek_bar_preference_title">seek_bar_preference</string></resources>

編輯完這兩個檔案,我們在SettingDemo.java中的onCreate中加入下面一句話就可以看到效果了
public class SettingDemo extends PreferenceActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.setting_demo); }}

效果如所示,根據大家很容易的理解布局檔案中的各個標籤的用途了

三.“深入瞭解”
現在我們點擊設定中的選項還沒有實質的效果,首先我們先來瞭解一下設定中的CheckBox控制項
private static final String CHECK_BOX_PRE = "check_box";//和xml中的key值對應private CheckBoxPreference mCheckBoxPre; //聲明一個用來控制checkbox的對象mCheckBoxPre = (CheckBoxPreference) findPreference(CHECK_BOX_PRE);//findPreference通過在xml中控制項的key值找到該控制項<CheckBoxPreference     android:key="check_box"android:title="@string/title_check_box" android:summary="@string/summary_check_box"android:persistent="false" />mCheckBoxPre.setOnPreferenceChangeListener(this);//設定監聽器我們將在onPreferenceChange中處理設定變化時的操作
詳細代碼如下:
public class SettingDemo extends PreferenceActivity implementsOnPreferenceChangeListener {private static final String CHECK_BOX_PRE = "check_box";private CheckBoxPreference mCheckBoxPre;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.setting_demo);mCheckBoxPre = (CheckBoxPreference) findPreference(CHECK_BOX_PRE);mCheckBoxPre.setOnPreferenceChangeListener(this);}@Overridepublic boolean onPreferenceChange(Preference preference, Object newValue) {// TODO Auto-generated method stubif (preference.getKey().equals(CHECK_BOX_PRE)) {mCheckBoxPre.setChecked((Boolean) newValue);mCheckBoxPre.setSummary((Boolean) newValue ? "checked": "unchecked");doSomething((Boolean) newValue ? 1 : 0);}return false;}private void doSomething(int i) {// TODO Auto-generated method stubif (i == 1) {Toast.makeText(this, "checked!", Toast.LENGTH_SHORT).show();} else if (i == 0) {Toast.makeText(this, "unchecked!", Toast.LENGTH_SHORT).show();}}}
運行起來看看效果吧!

在程式中我們也可以動態控制設定中內容的顯示

 

 Preference yourSetting = findPreference(KEY_YOUR_SETTING);

 getPreferenceScreen().removePreference(yourSetting );

 

在xml中我們也可以通過Intent來啟動另外的Activity

 

 <PreferenceScreen

            android:title="@string/manageapplications_settings_title"

            android:summary="@string/manageapplications_settings_summary">

        <intent android:action="android.intent.action.MAIN"

                android:targetPackage="com.android.settings"

                android:targetClass="com.android.settings.ManageApplications" />

    </PreferenceScreen>

 

通過指定包名和類名來啟動指定的Activity

當然也可以隱式的通過指定Action來啟動能接收該Intent的Activity,大家搜尋Intent會找到詳細的解釋,這裡就不累述了

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.