SharedPreferences 資料持久化

2021-08-19 23:34:31 字數 947 閱讀 8859

sharedpreferences  是乙個輕量級的資料儲存方式,

它是android資料持久化最簡單的一種,是以xml檔案儲存鍵值對資料,

可以儲存一些簡單的配置資訊,例如登入

因為sharedpreferences本身是乙個介面,程式無法直接建立sharedpreferences的例項。

使用:

public static sharedpreferences getsp(context context)
第乙個引數是檔名,第二個引數是讀寫型別

context.mode_private:預設操作模式,代表該檔案為私有模式,只能被應用本身訪問(值為0)

/**** 

清除所有

sp*/

public static void

cleardatas(context context)

/***

根據鍵刪除

* */

public static void

removedata(context context,string key)

/**

* @param

context

* @param

key*

@param

value

放置string

的值*/

public static void

putstring(context context, string key, string value)

/***

@param

context

* @param

key

獲取string

的值*/

public static string getstring(context context, string key)

sharedpreferences資料儲存

例項134 使用sharedpreferences儲存使用者輸入的使用者名稱和密碼,並在第二個activity中顯示 sharedpreferences 類供開發人員儲存和獲取基本資料型別的鍵值對,該類主要用於基本型別,如booleans,floats,ints,longs,strings。在應用程...

SharedPreferences資料儲存

1 23 android orientation vertical 4 android layout width fill parent 5 android layout height fill parent 6 78 android id id textview01 9 android layou...

讀寫SharedPreferences中的資料

很多時候我們開發的軟體需要向使用者提供軟體引數設定功能,例如我們常用的 qq,使用者可以設定是否允許陌生人新增自己為好友。對於軟體配置引數的儲存,如果是 window軟體通常我們會採用 ini檔案進行儲存,如果是 j2se應用,我們會採用 properties屬性檔案進行儲存。如果是 android...