Android 数据存储之SharedPreference

一、主要方法:

(1) getSharedPreferences

此方法获取 SharedPreference 的对象,通过调用 Activity 类的 getSharedPreferences 

方法获取 SharedPreference 对象的实例。

该方法第一个参数为保存的文件名,第二个参数为获取数据的格式,常用格式如下:

* Context.MODE_PRIVATE:指定该 SharedPreferences 数据只能被本应用程序读、写。

* Context.MODE_WORLD_READABLE:能被其他应用程序读,但不能写。

* Context.MODE_WORLD_WRITEABLE:能被其他应用程序读写。

(2) editor

获得 SharedPreference.Editor 对象,用于对数据进行存储和提交等。

(3) putxxx

通过键值对,将数据保存到XML文件中

(4) commit

用于数据的提交,对由 putxxx 提供的key-value 对进行保存。

(5) SharedPreferences.getxxx

获取保存的 key-value 对。


二、实例

本例程序在 OnCreate 方法中得到上次保存的数据,然后在程序退出时执行的 onStop 方法

保存本次操作的值并进行提交,代码如下:

//MainActivity.java

package com.example.sharedpreference;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.EditText;

public class MainActivity extends Activity {

	private final static String TABLE_NAME = "table";
	private EditText txtName, txtCollage;
	private CheckBox cbWork, cbMarry;
	private SharedPreferences mySharedPerferences;
	private SharedPreferences.Editor editor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取 SharedPerferences 的实例,同时创建 table.xml 文件
		mySharedPerferences = getSharedPreferences(
			TABLE_NAME, Activity.MODE_PRIVATE);
		//获取 SharedPreferences.Editor 的实例
		editor = mySharedPerferences.edit();
		//定义 EditText 接收姓名
		txtName = (EditText)findViewById(R.id.editText1);
		txtCollage = (EditText)findViewById(R.id.editText2);
		//定义 CheckBox 判断是否工作
		cbWork = (CheckBox)findViewById(R.id.checkBox1);
		cbMarry = (CheckBox)findViewById(R.id.checkBox2);
		// 获取数据
		// 获取 table.xml 中的姓名的数据,并对 txtName EditText 进行赋值
		// name key 值
		txtName.setText(mySharedPerferences.getString("name", ""));
		// 获取 table.xml 中学校数据,并赋值
		txtCollage.setText(mySharedPerferences.getString("collage", ""));
		
		cbWork.setChecked(mySharedPerferences.getBoolean("work", false));
		cbMarry.setChecked(mySharedPerferences.getBoolean("marry", false));
	}

	@Override
	protected void onStop() {
		super.onStop();
		//保存 txtName EditText 数据
		editor.putString("name", txtName.getText().toString());
		editor.putString("collage", txtCollage.getText().toString());
		//保存 cbwork 的选择状态
		editor.putBoolean("work", cbWork.isChecked());
		editor.putBoolean("marry", cbMarry.isChecked());
		//提交数据到 table.xml
		editor.commit();
	}
}

布局文件

<!-- activity_main.xml  -->
<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="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <requestFocus />
    </EditText>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="毕业院校" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="是否工作" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="是否结婚" />

</LinearLayout>
此方法将文件保存至 /data/data/"package name"/shared_prefs/table.xml。

在 eclipse 中打开 DDMS 视图,在File Explorer 下就可看到:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值