SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置比如窗口状态,一般在Activity中重载窗口状态onSaveInstanceState保存一般使用SharedPreferences完成,它提供了Android平台常规的Long整形、Int整形、String字符串型的保存。这种方式应该是用起来最简单的Android读写外部数据的方法了。以一种简单、透明的方式来保存一些用户个性化设置的字体、颜色、位置等参数信息。
在Android系统中,这些信息以XML文件的形式保存在
/data/data/PACKAGE_NAME /shared_prefs 目录下。
SharedPreferences pre = getSharedPreferences("soft",
Context.MODE_WORLD_READABLE);
在这里我们可以调用 activity 为我们提供的方法,这个方法有两个参数:
1)文件名 。 在这里要特别注意 。 因为在 Android 中已经确定了 SharedPreferences 是以 xm l形式保存,所以,在填写文件名参数时,不要给定 ” .xml ” 后缀, android 会自动添加 。它是采用键值对的形式保存参数。 当你需要获得某个参数值时 , 按照参数的键索引即可。
2)第二个可以理解为创建模式和之前的文件存储的模式是一样的。
Context. MODE_PRIVATE
Context. MODE_APPEND MODE_APPEND
Context. MODE_WORLD_READABLE
Context. MODE_WORLD_WRITEABLE
下面介绍一个实例来演示向SharedPreferences的读写
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/nameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:text="姓 名"
android:textSize="25dp"
/>
<EditText
android:id="@+id/nameET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/ageTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:text="年 龄"
android:textSize="25dp"
/>
<EditText
android:id="@+id/ageET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="100dp"
android:text="保存"
/>
<Button
android:id="@+id/btn2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="读取"
/>
</LinearLayout>
</LinearLayout>
package cn.class3g.activity;
import android.app.Activity;
import android.content.Context;
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;
import android.widget.Toast;
public class SharedPreferesActivity extends Activity implements
OnClickListener {
Button btn1, btn2;
EditText nameET, ageET;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
}
private void findViews() {
nameET = (EditText) this.findViewById(R.id.nameET);
ageET = (EditText) this.findViewById(R.id.ageET);
btn1 = (Button) this.findViewById(R.id.btn1);
btn2 = (Button) this.findViewById(R.id.btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name = "无名氏";
int age = -1;
SharedPreferences
shared = this.getSharedPreferences("info",Context.MODE_WORLD_READABLE );
switch (v.getId()) {
case R.id.btn1:
name = nameET.getText().toString().trim();
age = Integer.valueOf(ageET.getText().toString().trim());
Editor editor = shared.edit();
editor.putString("name", name);
editor.putInt("age", age);
// 保证操作的事务完整性
editor.commit();
break;
case R.id.btn2:
name = shared.getString("name", "无名氏");
age = shared.getInt("age", -1);
Toast.makeText(this,"name="+name+"age="+age, Toast.LENGTH_LONG).show();
break;
}
}
}
效果图展示: