在Android 中我们常常会存储一些比较小的数据,例如游戏的临时配置,和临时的用户名 密码 这时我们就会用到 SharedPreferences来存储一些小数据,他常常被成为轻量级的数据存储类,SharedPreferences如果你不是很理解你就将它理解为 网页里面的 Cookie 其实他的性质就类似与Cookie。 SharedPreferences他是以键值对来存储应用程序的配置信息的一种方式,它只能存储基本数据类型。 一个程序的配置文件仅可以在本应用程序中使用,或者说只能在同一个包内使用,不能在不同的包之间使用(这里有异议因为很多的地方解释的不是很清楚,有的说是可以在外部访问有的说不能~在不同的Activity中是可以使用的)。 实际上 SharedPreferences 是采用了 XML 格式将数据存储到设备中, DDMS 中的 File 在 Explorer 中的/data/data/<package name>/shares_prefs 下
下面给大家看一个例子来帮助大家理解:
Activity.java 为:
package cn.bailu;
import android.app.Activity;
import android.content.SharedPreferences;
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 SharedPreferencesActivity extends Activity {
private EditText name_et;
private EditText pass_et;
private Button save_btn;
private Button read_btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showViews();
}
private void showViews() {
name_et = (EditText) this.findViewById(R.id.name_et);//获取name_et EditText 中的 数据 也就是 main.xml 中id为 name_et 的组件数据;
pass_et = (EditText) this.findViewById(R.id.pass_et);//获取pass_et EditText 中的 数据 同上
save_btn = (Button) this.findViewById(R.id.save_btn);//获取save_btn Button 中的 数据 同上
read_btn = (Button) this.findViewById(R.id.read_btn);//获取read_btn Button中的 数据 同上
//存入数据
//为 main.xml 中 id 为 save_btn 的按钮设置监听
save_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//将数据转换为String 型的数据
String name =name_et.getText().toString().trim();
String pass = pass_et.getText().toString().trim();
//Toast.makeText(SharedPreferencesActivity.this, "姓名:"+name+" "+"密码:"+pass, Toast.LENGTH_SHORT).show();
/*将数据存入xml中,名称为 PersonData 后面的MODE_PRIVATE 为SharedPreferences 的访问权限
Context.MODE_PRIVATE //私有
Context.MODE_APPEND //追加
Context.MODE_WORLD_READABLE //全世界读 也就是外部的(其他的)Activity 也可以去访问此数据
Context.MODE_WORLD_WRITEABLE //这里是全世界 写 也就是 外部的(其他的)Activity 也可以去访问数据
*/
SharedPreferences shared = getSharedPreferences("PersonData",MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("name", name);
editor.putString("password",pass );
editor.commit();
}
});
//读取数据
read_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/*将数据存入xml中,名称为 PersonData 后面的MODE_PRIVATE 为SharedPreferences 的访问权限
Context.MODE_PRIVATE //私有
Context.MODE_APPEND //追加
Context.MODE_WORLD_READABLE //全世界读 也就是外部的(其他的)Activity 也可以去访问此数据
Context.MODE_WORLD_WRITEABLE //这里是全世界 写 也就是 外部的(其他的)Activity 也可以去访问数据
*/
SharedPreferences shared = getSharedPreferences("PersonData", 0);
String name2 = shared.getString("name", "");//这里是将 main.xml 中的数据读出来
String pass2 = shared.getString("password", ""); //同上
Toast.makeText(SharedPreferencesActivity.this, "姓名:"+name2+" "+"密码:"+pass2, Toast.LENGTH_SHORT).show();
}
});
}
}
这里是xml文档:
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="姓名" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name_et"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/pass_et"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存"
android:id="@+id/save_btn"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="读取"
android:id="@+id/read_btn"/>
</LinearLayout>
转载请注明出处:blog.csdn.net/bailu66
Power by 白璐.中国