Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 特殊碎片(十三):PreferenceFragment 設定頁實作

編輯:關於 Android 程式設計

幾乎每款稍具規模的 App,都需要提供一個「設定」頁面,讓使用者依自己的習慣調整行為——例如要不要自動儲存登入資訊、頁面每隔幾秒刷新一次等等。在 Android 上,早期我們用 PreferenceActivity 來快速打造這種選項清單;而在 Android 3.0 以後,更推薦使用 PreferenceFragment,讓設定界面能靈活嵌入不同的 Activity。

步驟一:定義 preferences.xml

先在 res 下新增一個 xml 資料夾,並建立 preferences.xml。設定界面的樣貌幾乎全靠這支 XML 描述,內容用各種 Preference 元件堆疊而成:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="https://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="Category 1">
        <CheckBoxPreference
            android:title="Checkbox"
            android:defaultValue="false"
            android:summary="True of False"
            android:key="checkboxPref" />
    </PreferenceCategory>

    <PreferenceCategory android:title="Category 2">
        <EditTextPreference
            android:name="EditText"
            android:summary="Enter a string"
            android:defaultValue="[Enter a string here]"
            android:title="Edit Text"
            android:key="editTextPref" />
        <RingtonePreference
            android:name="Ringtone Preference"
            android:summary="Select a ringtone"
            android:title="Ringtones"
            android:key="ringtonePref" />
        <PreferenceScreen
            android:title="Second Preference Screen"
            android:summary="Click here to go to the second Preference Screen"
            android:key="secondPrefScreenPref">
            <EditTextPreference
                android:name="EditText"
                android:summary="Enter a string"
                android:title="Edit Text (second Screen)"
                android:key="secondEditTextPref" />
        </PreferenceScreen>
    </PreferenceCategory>
</PreferenceScreen>

這裡用 PreferenceCategory 把選項分門別類,支援的元件相當豐富:核取方塊(CheckBoxPreference)、文字輸入(EditTextPreference)、鈴聲選擇(RingtonePreference),甚至能用巢狀的 PreferenceScreen 做出「點進去還有下一層設定」的多層選單。

步驟二:建立 PreferenceFragment 子類

接著建立一個繼承 PreferenceFragment 的類別,並在 onCreate 中用 addPreferencesFromResource() 把剛才的 XML 載入進來:

public class Fragment1 extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 從 xml 檔案載入選項
        addPreferencesFromResource(R.xml.preferences);
    }
}

步驟三:在 Activity 中掛上 Fragment

最後,透過 FragmentManager 與 FragmentTransaction,把 Fragment1 放進 Activity 的內容區:

public class PreferenceFragmentExampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment1 fragment1 = new Fragment1();
        fragmentTransaction.replace(android.R.id.content, fragment1);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}

實作後的效果

按 F11 執行後,你會看到一個標準的設定清單。點擊「第二層設定」那一列,就會展開下一個 PreferenceScreen。此時若用 DDMS 工具查看,會發現應用的資料目錄下自動產生了一個存放設定值的 XML 檔案——這正是 Preference 機制幫我們把使用者選擇持久化的成果,完全不需要另外寫儲存邏輯。

用 PreferenceFragment 打造設定頁的優點在於:它自帶持久化、樣式統一,又能像一般 Fragment 一樣自由嵌入平板或手機的不同版面。掌握了它,設定界面這塊拼圖就完成了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved