您的位置:首页 > 移动开发 > Android开发

Android SharedPreferences

2011-06-09 15:21 267 查看
SharedPreferences是一种轻量级的数据存储方式,学过Web开发的同学,可以想象它是一个小小的Cookie。它可以用键值对的方式把简单数据类型(boolean、int、float、long和String)存储在应用程序的私有目录下(data/data/包名/shared_prefs/)自己定义的xml文件中。
以下表格为获取SharedPreferences对象的两个方法:
返回值
函数
备注
SharedPreferences
Context.getSharedPreferences(String
name,int mode)
name为本组件的配置文件名(如果想要与本应用程序的其他组件共享此配置文件,可以用这个名字来检索到这个配置文件)。
mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。
SharedPreferences
Activity.getPreferences(int
mode)
配置文件仅可以被调用的Activity使用。
mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。
如果要读取配置文件信息,只需要直接使用SharedPreferences对象的getXXX()方法即可,而如果要写入配置信息,则必须先调用SharedPreferences对象的edit()方法,使其处于可编辑状态,然后再调用putXXX()方法写入配置信息,最后调用commit()方法提交更改后的配置文件。

利用SharedPreferences读取保存的配置,需要先声明一个SharedPreferences变量,然后getSharedPreferences得到一个SharedPreferences实例对象,即得到一个已经指定好指向哪个文件、以哪种方式指向的SharedPreferences对象;然后通过getXX得到保存在配置文件里面的键值对,XX表示保存的值的类型。代码如下:
SharedPreferences preferences; preferences = getSharedPreferences("fileName", FileList.MODE_PRIVATE); //其中fileName表示存放配置的配置文件名字
/*
"modle"是保存配置文件的键值对的键;“你好”则是当读取当前键如果是空的话,返回的默认值,可以是任何相应类型的数据
*/
preferences.getString("modle", "你好");


利用SharedPreferences保存用户的配置,同样需要有一个已经实例化指向配置的文件的SharedPreferences对象;然后使用SharedPreferences接口的edit获得SharedPreferences.Editor对象;再利用Editor对象的putXX方法(xx同样表示值的数据类型)把数据存到键值对中;最后利用Editor对象的commit方法,把这些键值对保存到SharedPreferences对象指向的配置文件中。代码如下:
SharedPreferences.Editor editor = preferences.edit();
//保存组件中的值
editor.putString("modle", "value");
//提交保存的结果
editor.commit();


到此,我们就已经通过SharedPreferences把配置信息保存到了配置文件中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Andy's Blog!"
android:textSize="16sp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帐号:"/>
<EditText
android:id="@+id/Username"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:id="@+id/Password"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="textPassword"/>
</LinearLayout>
<CheckBox
android:id="@+id/IsChecked"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="记住密码"/>
<Button android:id="@+id/LoginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"/>
</LinearLayout>


package com.king.store;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
public class SharedActivity extends Activity {
private final String file_Name="userinfo";
private EditText username,psw;
private CheckBox cbRem;
private String userName,passWord;
private Boolean isRem=false;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

username=(EditText)findViewById(R.id.Username);
psw=(EditText)findViewById(R.id.Password);

cbRem=(CheckBox)findViewById(R.id.IsChecked);

cbRem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
isRem=isChecked;
}
});

SharedPreferences sharedP=getSharedPreferences(file_Name, 0);
username.setText(sharedP.getString("UserName", null));
cbRem.setChecked(sharedP.getBoolean("Remember", true));
if(cbRem.isChecked()){
psw.setText(sharedP.getString("Password", null));
}else{
psw.setText(null);
}
}

public void onStop(){
super.onStop();
SharedPreferences storeP=getSharedPreferences(file_Name, 0);
SharedPreferences.Editor editor=storeP.edit();

userName=username.getText().toString();
passWord=psw.getText().toString();
editor.putString("UserName", userName);
editor.putString("Password", passWord);
editor.putBoolean("Remember", isRem);
editor.commit();
}
}


以上的程序是通过创建一个
userinfo.xml 文件来存储用户退出时保存用户的一些基本信息。当再次打开程序时,是通过读取该文件来获取之前的信息。

说明:使用SharedPreferences存取数据

保存key-value对一般要指定一个文件名,然后用类似putString的方法指定key和value。SharedPreferences也采用了同样的方法。使用SharedPreferences保存key-value对的步骤如下:

(1) 使用Activity类的getSharedPreferences方法获得SharedPreferences对象。其中存储key-value的文件名的名称由getSharedPreferences方法的第一个参数指定。

(2) 使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。

(3) 通过SharedPreferences.Editor接口的putXXX方法保存key-value对。其中XXX表示value的不同数据类型。Boolean类型的value则是用putBoolean方法,字符串类型的则为putString方法。

(4) 通过SharedPreferences.Editor接口的commit方法保存key-value对。Commit方法相当于数据库事务中的提交 (commit)操作。只有在事件结束后进行提交,才会将数据真正保存在数据库中。保存key-value也是一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: