见归档项目:SharedPreferencesDemo.zip
1、对于数据量较小,且有明显的K-V形式的数据而言,适合用SharedPreferences保存。SharedPreferences的数据以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数据的基本步骤
(1)获取SharedPreferences实例
(2)通过SharedPreferences的实例获取Editor实例。注:SharedPreferences本身并没有提供写入数据的能力,而是通过其内部类SharedPreferences.Editor来实现。
(3)调用Editor的write方法,切记后面还要commit。
3、读取SharedPreferences数据的基本步骤
(1)获取SharedPreferences实例
(2)读取单一数据:调用SharedPreferences的getXxx()方法,分别读取String,int,long等类型。
(3)读取全量数据:调用SharedPreferences的getAll()方法,再遍历map。
实例:
图形界面如下
<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" >
<EditText
android:id="@+id/et_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/key" />
<EditText
android:id="@+id/et_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/value" />
<Button
android:id="@+id/bt_write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/write" />
<Button
android:id="@+id/bt_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read" />
<TextView
android: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 {
@Override
protected 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是一个接口,程序无法直接创建SharedPreferences对象,只能通过Context提供的getSharedPreferences()方法。
final SharedPreferences preference = getSharedPreferences("test", MODE_PRIVATE);
//2、获取SharedPreferences。Editor对象,用于写数据。本步骤只对写数据有必要。
final SharedPreferences.Editor editor = preference.edit();
btWrite.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String key = etKey.getText().toString().trim();
String value = etValue.getText().toString().trim();
//3、写入数据并commit。
editor.putString(key, value);
editor.commit();
}
});
btRead.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//2、根据key值读取单一数据
/* String content = preference.getString("name", "lujinhong");
tvContent.setText(content);*/
//2、读取所有数据
String content = "";
Map<String, ?> allContent = preference.getAll();
//注意遍历map的方法
for(Map.Entry<String, ?> entry : allContent.entrySet()){
content+=(entry.getKey()+entry.getValue());
}
tvContent.setText(content);
}
});
}
}