日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SharedPreferences基础

發(fā)布時(shí)間:2024/1/23 编程问答 96 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SharedPreferences基础 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

見歸檔項(xiàng)目:SharedPreferencesDemo.zip


1、對于數(shù)據(jù)量較小,且有明顯的K-V形式的數(shù)據(jù)而言,適合用SharedPreferences保存。SharedPreferences的數(shù)據(jù)以xml文件的形式保存在/data/data/包名/SharedPreferences的目錄下,如下例:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map><string name="hi3">liaoliuqing</string><string name="hi">liaoliuqing</string><string name="name">liaoliuqing</string> </map>


2、寫入SharedPreferences數(shù)據(jù)的基本步驟

(1)獲取SharedPreferences實(shí)例

(2)通過SharedPreferences的實(shí)例獲取Editor實(shí)例。注:SharedPreferences本身并沒有提供寫入數(shù)據(jù)的能力,而是通過其內(nèi)部類SharedPreferences.Editor來實(shí)現(xiàn)。

(3)調(diào)用Editor的write方法,切記后面還要commit。


3、讀取SharedPreferences數(shù)據(jù)的基本步驟

(1)獲取SharedPreferences實(shí)例

(2)讀取單一數(shù)據(jù):調(diào)用SharedPreferences的getXxx()方法,分別讀取String,int,long等類型。

(3)讀取全量數(shù)據(jù):調(diào)用SharedPreferences的getAll()方法,再遍歷map。


實(shí)例:

圖形界面如下



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"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" ><EditTextandroid:id="@+id/et_key"android:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="text"android:hint="@string/key" /><EditTextandroid:id="@+id/et_value"android:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="text"android:hint="@string/value" /><Buttonandroid:id="@+id/bt_write"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/write" /><Buttonandroid:id="@+id/bt_read"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/read" /><TextViewandroid:id="@+id/tv_content"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" /></LinearLayout>
package com.ljh.sharepreferencedemo;import java.util.Map; import java.util.Map.Entry;import android.os.Bundle; import android.app.Activity; import android.content.SharedPreferences; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final EditText etKey = (EditText) findViewById(R.id.et_key);final EditText etValue = (EditText) findViewById(R.id.et_value);Button btRead = (Button) findViewById(R.id.bt_read);Button btWrite = (Button) findViewById(R.id.bt_write);final TextView tvContent = (TextView) findViewById(R.id.tv_content);//1、獲取SharedPreferences對象。SharedPreferences是一個(gè)接口,程序無法直接創(chuàng)建SharedPreferences對象,只能通過Context提供的getSharedPreferences()方法。final SharedPreferences preference = getSharedPreferences("test", MODE_PRIVATE);//2、獲取SharedPreferences。Editor對象,用于寫數(shù)據(jù)。本步驟只對寫數(shù)據(jù)有必要。final SharedPreferences.Editor editor = preference.edit();btWrite.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {String key = etKey.getText().toString().trim();String value = etValue.getText().toString().trim();//3、寫入數(shù)據(jù)并commit。editor.putString(key, value);editor.commit();}});btRead.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {//2、根據(jù)key值讀取單一數(shù)據(jù) /* String content = preference.getString("name", "lujinhong");tvContent.setText(content);*///2、讀取所有數(shù)據(jù)String content = "";Map<String, ?> allContent = preference.getAll();//注意遍歷map的方法for(Map.Entry<String, ?> entry : allContent.entrySet()){content+=(entry.getKey()+entry.getValue());}tvContent.setText(content);}});}}






總結(jié)

以上是生活随笔為你收集整理的SharedPreferences基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。