Android之SharedPreferences資料存放區

來源:互聯網
上載者:User

SharedPreferences也是一種輕型的資料存放區方式,它的本質是基於XML檔案儲存體key-value索引值對資料,通常用來儲存一些簡單的配置資訊。其儲存位置在/data/data/<包名>/shared_prefs目錄下。SharedPreferences對象本身只能擷取資料而不支援儲存和修改,儲存修改是通過Editor對象實現。

SharedPreferences常用的屬性和方法

方法名稱

描述

public abstract boolean contains (String key)

判斷SharedPreferences是否包含特定key的資料

public abstract SharedPreferences.Editor edit ()

返回一個Edit對象用於操作SharedPreferences

public abstract Map<String, ?> getAll ()

擷取SharedPreferences資料裡全部的key-value對

getXXX(String key,XXX defvlaue)

擷取SharedPreferences資料指定key所對應的value,如果該key不存在,返回預設值defValue。其中XXX可以是boolean、float、int、long、String等基本類型的值

由於SharedPreference是一個介面,而且在這個介面裡並沒有提供寫入資料和讀取資料的能力。但是在其內部有一個Editor內部的介面,Edit這個介面有一系列的方法用於操作SharedPreference。

Editor介面的常用方法

方法名稱

描述

public abstract SharedPreferences.Editor clear ()

清空SharedPreferences裡所有的資料

public abstract boolean commit ()

當Editor編輯完成後,調用該方法可以提交修改,而且必須要調用這個資料才修改

public abstract SharedPreferences.Editor putXXX (String key, boolean XXX)

向SharedPreferences存入指定的key對應的資料,其中XXX可以是boolean、float、int、long、String等基本類型的值

public abstract SharedPreferences.Editor remove (String key)

刪除SharedPreferences裡指定key對應的資料項目

 

SharedPreferences是一個介面,程式是無法建立SharedPreferences執行個體的,可以通過Context.getSharedPreferences(String name,int mode)來得到一個SharedPreferences執行個體

name:是指檔案名稱,不需要加尾碼.xml,系統會自動為我們添加上。一般這個檔案儲存體在/data/data/<package name>/shared_prefs下(這個面試常問到)

mode:是指定讀寫方式,其值有三種,分別為:

Context.MODE_PRIVATE:指定該SharedPreferences資料只能被本應用程式讀、寫

Context.MODE_WORLD_READABLE:指定該SharedPreferences資料能被其他應用程式讀,但不能寫

Context.MODE_WORLD_WRITEABLE:指定該SharedPreferences資料能被其他應用程式讀寫。

實現SharedPreferences儲存的步驟如下:

  一、根據Context擷取SharedPreferences對象

  二、利用edit()方法擷取Editor對象。

  三、通過EditorObject Storage Servicekey-value索引值對資料。

  四、通過commit()方法提交資料。

  具體實現代碼如下:

package com.example.sharedpreferences;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {EditText nameEditText;EditText sexEditText;String nameStr;String sexStr;SharedPreferences sp;Editor editor;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button writeButton = (Button)findViewById(R.id.write);Button readButton = (Button)findViewById(R.id.read);nameEditText = (EditText)findViewById(R.id.name);sexEditText = (EditText)findViewById(R.id.sex);sp = getSharedPreferences("SP", MODE_PRIVATE);  //!!!!editor = sp.edit();                             //!!!!writeButton.setOnClickListener(new OnClickListener() {/** 存入資料*/@Overridepublic void onClick(View v) {nameStr = nameEditText.getText().toString();sexStr = sexEditText.getText().toString();editor.putString("name_key", nameStr);   //!!!!editor.putString("sex_key", sexStr);     //!!!!editor.commit(); //!!!!nameEditText.setText(null);sexEditText.setText(null);}});readButton.setOnClickListener(new OnClickListener() {/** 取出資料*/@Overridepublic void onClick(View v) {sexEditText.setText(sp.getString("sex_key", "none"));  //!!!!nameEditText.setText(sp.getString("name_key", "none"));//!!!!}});}}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#000000"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="#FFFFFF"            android:layout_margin="10dp"            android:text="姓名:"/>        <EditText             android:id="@+id/name"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#FFFFFF"            android:textColor="#000000"            android:layout_margin="10dp"            android:hint="空"/>    </LinearLayout>    <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="#FFFFFF"            android:layout_margin="10dp"            android:text="性別:"/>        <EditText             android:id="@+id/sex"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#FFFFFF"            android:textColor="#000000"            android:layout_margin="10dp"            android:hint="空"/>    </LinearLayout><LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">    <Button         android:id="@+id/write"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#FFFFFF"        android:layout_margin="10dp"        android:text="  寫入   "/>    <Button         android:id="@+id/read"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#FFFFFF"        android:layout_margin="10dp"        android:text="  讀取   "/>        </LinearLayout></LinearLayout>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.