Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 輕量級資料儲存:SharedPreferences 入門

在 Android 開發中,若要保存一些簡單的設定,例如視窗狀態、字型、顏色或位置等使用者偏好,SharedPreferences 是最輕便的選擇。本文將說明它的運作原理與讀寫方式,並提供一個完整範例。

SharedPreferences 是什麼

SharedPreferences 是 Android 平台上的一個輕量級儲存類別,主要以鍵值對的形式保存常用的設定資料,並支援 Long、Int、String 等型別。一般在 Activity 中重載視窗狀態的 onSaveInstanceState 時,就會用到它。可以說,這是最簡單的 Android 讀寫外部資料方式,能以簡單、透明的手法保存使用者個性化的參數。

在 Android 系統中,這些資訊會以 XML 檔的形式保存在:

/data/data/PACKAGE_NAME/shared_prefs

取得 SharedPreferences 物件的方式如下:

SharedPreferences pre = getSharedPreferences("soft", Context.MODE_WORLD_READABLE);

這個方法有兩個參數:

  1. 檔名:這裡要特別注意,由於 Android 已固定以 XML 形式保存,因此填寫檔名時不要加上「.xml」後綴,系統會自動補上。資料以鍵值對形式保存,需要時只要依鍵索引即可取值。
  2. 建立模式:與檔案儲存模式相同,常見的有 MODE_PRIVATE、MODE_APPEND、MODE_WORLD_READABLE、MODE_WORLD_WRITEABLE 等。

讀寫範例

以下版面包含姓名與年齡兩個輸入欄位,以及「保存」與「讀取」兩顆按鈕:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/nameTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:text="姓 名"
            android:textSize="25dp" />
        <EditText
            android:id="@+id/nameET"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/ageTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:text="年 齡"
            android:textSize="25dp" />
        <EditText
            android:id="@+id/ageET"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btn1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="100dp"
            android:text="保存" />
        <Button
            android:id="@+id/btn2"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:text="讀取" />
    </LinearLayout>
</LinearLayout>

在程式碼中,按下「保存」時透過 getSharedPreferences 取得物件,用 Editor 寫入資料並呼叫 commit() 提交;按下「讀取」時,再以相同的鍵取值即可還原畫面。就是這麼簡單,SharedPreferences 讓輕量資料的持久化變得輕而易舉。

Copyright © Linux教程網 All Rights Reserved