SharedPreferences是SDK中的一個自帶類,可以完成簡單的數據存儲。
一、特點:
1.是一種輕量級的數據存儲方式
2.本質是基於XML(key-value)的形式存儲的
3.通常用於存儲一些簡單的配置信息,如用戶名和密碼
二、操作:
1.讀取數據:SharedPreferences的對象來完成
2.更新數據:Editor的接口對象來完成
三、代碼實現:
1.獲取SharedPreferences對象
a) 獲取安卓默認的pref對象,會建立/data/data/包名/shared_pref/xxx.xml文件
//參數:上下文對象
SharedPreferences pref=PreferenceManeger.getDefaultSharedPreferences(this);
b) 自定義獲取pref對象 xxx.xml的名稱,對該xml的操作權限
SharedPreferences pref=getSharedPreferences("pref",MODE_PRIVATE);
2.獲得Editor對象
a) Editor editor=pref.edit();
3.更新數據
editor.putString("name","張三");
editor.putInt("age",19);
editor.putBoolean("default",true);
editor.remove("default");
editor.commit();
4.獲得數據
String name=pref.getString("name");
int age=pref.getInt("age");
總結:xml中的數據除了editor.clear()和在手機應用程序中清空數據會一直有效!!!
我在寫一個小Demo的時候,在onCreate外面初始化pref一直報空指針異常....!!!
搞了好久才發現,只有onCreate之後,纔會有當前(this)對象!!!
MainActivity.java
package com.example.dsd;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class MainActivity extends Activity {
private EditText mEtInput;
private TextView mTvShow;
private Button mBtnSave;
private Button mBtnShow;
private Button mBtnClear;
private SharedPreferences pref;
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = PreferenceManager.getDefaultSharedPreferences(this);
editor = pref.edit();
mEtInput = (EditText) findViewById(R.id.et_input);
mTvShow = (TextView) findViewById(R.id.tv_show);
mBtnSave = (Button) findViewById(R.id.btn_save);
mBtnShow = (Button) findViewById(R.id.btn_show);
mBtnClear = (Button) findViewById(R.id.btn_clear);
mBtnSave.setOnClickListener(listener);
mBtnShow.setOnClickListener(listener);
mBtnClear.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_save:
editor.putString("content", mEtInput.getText().toString());
break;
case R.id.btn_show:
mTvShow.setText(pref.getString("content", "您還未存儲").trim());
break;
case R.id.btn_clear:
editor.clear();
break;
}
editor.commit();
}
};
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存" />
<Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="展示" />
<Button
android:id="@+id/btn_clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清空" />
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>