SharedPreferences是 Android中存储简单数据的一个工具类。可以想象它是一个小小的Cookie,它通过用键值对的 方式把简单数据类型(boolean、int、float、long和String)存储在应用程序的私有目录下(data/data/包名 /shared_prefs/)自己定义的xml文件中。

一、简介
  它提供一种轻量级的数据存储方式,通过eidt()方法来修改里面的内容,通过Commit()方法来提交修改后的内容。

二、重要方法
public abstract boolean contains (String key) :检查是否已存在该文件,其中key是xml的文件名。
edit ():为preferences创建一个编辑器Editor,通过创建的Editor可以修改preferences里面的数据,但必须执行commit()方法。 getAll ():返回preferences里面的多有数据。 getBoolean (String key, boolean defValue):获取Boolean型数据
getFloat (String key, float defValue):获取Float型数据
getInt (String key, int defValue):获取Int型数据
getLong (String key, long defValue):获取Long型数据
getString (String key, String defValue):获取String型数据
registerOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener):注册一个当 preference发生改变时被调用的回调函数。
unregisterOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener):删除当前回调函数。

三、重要接口SharedPreferences.Editor
  1.简介
  用于修改SharedPreferences对象的内容,所有更改都是在编辑器所做的批处理,而不是复制回原来的SharedPreferences或持久化存储,直到你调用commit(),才将持久化存储。

   2.重要方法
  clear ():清除内容。
  commit ():提交修改

  remove (String key):删除preference


java类

package com.android.activity;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText etAccount;private EditText etPW;private Button btnLogin;private Button btnExit;private CheckBox cbrp;private CheckBox cbal;// private UserMgr userMgr;// private User user;private SharedPreferences sp;// 相当于cookieprivate Button tvClear;/* * (non-Javadoc) *  * @see android.app.Activity#onCreate(android.os.Bundle) */@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.login);etAccount = (EditText) findViewById(R.id.etaccount);// 账号etPW = (EditText) findViewById(R.id.etpw);// 密码cbrp = (CheckBox) findViewById(R.id.cbrp);// 记住密码cbal = (CheckBox) findViewById(R.id.cbal);// 自动登陆btnLogin = (Button) findViewById(R.id.btnlogin);// 登陆btnExit = (Button) findViewById(R.id.btnexit);// 退出tvClear = (Button) findViewById(R.id.tvclear);// 清除缓存InitConfig();// 初始化控件cbrp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {sp = getSharedPreferences("UserInfo", 0);sp.edit().putBoolean("cbrp", isChecked).commit();}});cbal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {sp = getSharedPreferences("UserInfo", 0);sp.edit().putBoolean("cbal", isChecked).commit();}});// 如果选择自动登陆后,则直接登陆就行了,if (cbal.isChecked()) {// 选择了自动登陆// 判断密码是否正确sp = getSharedPreferences("UserInfo", 0);String password = sp.getString("password", null);Toast.makeText(MainActivity.this,"自动登陆时的密码是=" + sp.getString("password", null), 3000).show();if (null != password && password.equals("q")) {Toast.makeText(MainActivity.this, "自动登陆成功.....", 3000).show();Intent intent = new Intent(MainActivity.this,LoginSuccess.class);startActivity(intent);// finish();} else {Toast.makeText(MainActivity.this, "自动登陆失败.....", 3000).show();}}btnLogin.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (cbrp.isChecked()) {// 选择了记住密码sp = getSharedPreferences("UserInfo",Context.MODE_WORLD_WRITEABLE| Context.MODE_WORLD_READABLE);sp.edit().putString("account",etAccount.getText().toString()).commit();sp.edit().putString("password", etPW.getText().toString()).commit();Toast.makeText(MainActivity.this, "记住密码.....", 3000).show();} else {// 没有记住密码// 设置为空的.sp = getSharedPreferences("UserInfo", 0);sp.edit().putString("password", null).commit();Toast.makeText(MainActivity.this, "没有记住密码", 3000).show();}// 对数据进行校验,然后进入登陆成功页面if (etPW.getText().toString().equals("q")) {//Intent intent = new Intent(MainActivity.this,LoginSuccess.class);startActivity(intent);// finish();Toast.makeText(MainActivity.this, "登陆成功......", 3000).show();} else {Toast.makeText(MainActivity.this, "登陆失败...........", 3000).show();}}});btnExit.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {System.exit(0);}});tvClear.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "清除缓存成功.......", 3000).show();// 设置为空的.sp = getSharedPreferences("UserInfo", 0);sp.edit().clear().commit();}});}// 初始化配置private void InitConfig() {sp = getSharedPreferences("UserInfo", 0);etAccount.setText(sp.getString("account", null));etPW.setText(sp.getString("password", null));cbal.setChecked(sp.getBoolean("cbal", false));cbrp.setChecked(sp.getBoolean("cbrp", false));}}

xml:

<?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"><ImageView android:layout_width="fill_parent"android:layout_height="wrap_content" android:id="@+id/ivlogo" /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:layout_width="fill_parent"android:layout_height="wrap_content"><TextView android:layout_width="wrap_content"android:layout_marginLeft="20dip" android:gravity="center_vertical"android:layout_height="wrap_content" android:id="@+id/tvaccount"android:text="帐号:" android:textSize="20sp" /><EditText android:layout_width="70px" android:layout_height="wrap_content"android:id="@+id/etaccount" android:layout_marginRight="20dip"android:maxLength="20" /></LinearLayout><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"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:id="@+id/tvpw"android:layout_marginLeft="20dip" android:gravity="center_vertical"android:text="密码:" android:textSize="20sp" /><EditText android:layout_width="70px" android:layout_height="wrap_content"android:layout_marginRight="20dip" android:id="@+id/etpw"android:inputType="textPassword" /></LinearLayout><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content" android:orientation="horizontal"android:layout_marginTop="5dip" android:layout_marginRight="20dip"><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/tvclear"android:text="清除Cookies" android:textColor="#aa0000"android:textSize="12px" /></LinearLayout><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content" android:orientation="horizontal"android:layout_marginTop="5dip"><Button android:layout_width="100px" android:layout_height="wrap_content"android:id="@+id/btnlogin" android:layout_gravity="center"android:text="登录" /><Button android:layout_width="100px" android:layout_height="wrap_content"android:id="@+id/btnexit" android:layout_gravity="center"android:text="退出" /></LinearLayout><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content" android:orientation="horizontal"android:layout_marginTop="25dip"><CheckBox android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/cbrp"android:text="记住密码" android:textSize="12px" /><CheckBox android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/cbal"android:text="自动登录" android:textSize="12px" /></LinearLayout></LinearLayout>



更多相关文章

  1. 【Android】Handler的应用(一):从服务器端加载JSON数据
  2. Android的进阶学习(一)--Activity异常退出
  3. Android:Intent 显示和隐式 学习
  4. Android开发中如何使用绘制图表
  5. Android(安卓)Volley入门到精通:初识Volley的基本用法(示例,出错
  6. Android(安卓)View(二)-View的scrollTo()以及scrollBy()说明
  7. TabActivity实现多页显示效果
  8. okhttp3.x的post请求
  9. Android数据存储(二) Files

随机推荐

  1. Android(安卓)Jamendo开源在线音乐播放器
  2. Bluedroid的结构和代码分布
  3. Android开发者指南(18) —— Web Apps Ov
  4. Drawable的详解
  5. 编译kernel perl版本bug
  6. android 签名发布
  7. PackageManager的intent匹配查询流程
  8. Android(安卓)animation学习笔记之view/d
  9. 将Android下的可执行文件以静态库的形式
  10. android_atomic_dec android_atomic_inc