SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来存储一些轻量级的数据。

代码如下:

 //实例化SharedPreferences对象(第一步)
SharedPreferences mySharedPreferences= getSharedPreferences("test",
Activity.MODE_PRIVATE);
//实例化SharedPreferences.Editor对象(第二步)
SharedPreferences.Editor editor = mySharedPreferences.edit();
//用putString的方法保存数据
editor.putString("name", "Karl");
editor.putString("habit", "sleep");
//提交当前数据
editor.commit();
//使用toast信息提示框提示成功写入数据
Toast.makeText(this, "数据成功写入SharedPreferences!" ,
Toast.LENGTH_LONG).show();

执行以上代码,SharedPreferences将会把这些数据保存在test.xml文件中,可以在File Explorer的data/data下导出该文件,并查看。 
那么已经保存好的数据如何读取出来呢。我们来看:

 //同样,在读取SharedPreferences数据前要实例化出一个SharedPreferences对象
SharedPreferences sharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE);
// 使用getString方法获得value,注意第2个参数是value的默认值
String name =sharedPreferences.getString("name", "");
String habit =sharedPreferences.getString("habit", "");
//使用toast信息提示框显示信息
Toast.makeText(this, "读取数据如下:"+"\n"+"name:" + name + "\n" + "habit:" + habit, Toast.LENGTH_LONG).show();

程序源代码如下:

 public class Main extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void onClick_WriteData(View view)
{
SharedPreferences mySharedPreferences = getSharedPreferences("test",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("name", "karl");
editor.putString("habit", "sleep");
editor.commit();
Toast.makeText(this, "数据成功写入SharedPreferences!" ,
Toast.LENGTH_LONG).show(); }
public void onClick_ReadData(View view)
{
SharedPreferences sharedPreferences = getSharedPreferences("test",
Activity.MODE_PRIVATE);
String name = sharedPreferences.getString("name", "");
String habit = sharedPreferences.getString("habit", ""); Toast.makeText(this, "读取数据如下:"+"\n"+"name:" + name + "\n" + "habit:" + habit,
Toast.LENGTH_LONG).show(); }
}

运行结果如下:

同样,如果设置记住密码,自动登录等复选框,也是将用户输入的数据进行保存,保存在xml文件中,再从中进行读取,如果正确,直接进入下一个成功后的界面,当用户下一次进入时,首先判断输入的文本在xml中有没有记录,如果有记录,就直接从xml文件中读取,实现了记住密码的功能。这里不再进行详细阐述。

1:LoginActivity的代码如下:

 package com.liu.activity;

 import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast; public class LoginActivity extends Activity { private EditText userName, password;
private CheckBox rem_pw, auto_login;
private Button btn_login;
private ImageButton btnQuit;
private String userNameValue,passwordValue;
private SharedPreferences sp; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //去除标题
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login); //获得实例对象
sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
userName = (EditText) findViewById(R.id.et_zh);
password = (EditText) findViewById(R.id.et_mima);
rem_pw = (CheckBox) findViewById(R.id.cb_mima);
auto_login = (CheckBox) findViewById(R.id.cb_auto);
btn_login = (Button) findViewById(R.id.btn_login);
btnQuit = (ImageButton)findViewById(R.id.img_btn); //判断记住密码多选框的状态
if(sp.getBoolean("ISCHECK", false))
{
//设置默认是记录密码状态
rem_pw.setChecked(true);
userName.setText(sp.getString("USER_NAME", ""));
password.setText(sp.getString("PASSWORD", ""));
//判断自动登陆多选框状态
if(sp.getBoolean("AUTO_ISCHECK", false))
{
//设置默认是自动登录状态
auto_login.setChecked(true);
//跳转界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
}
} // 登录监听事件 现在默认为用户名为:xuyinghui 密码:123
btn_login.setOnClickListener(new OnClickListener() { public void onClick(View v) {
userNameValue = userName.getText().toString();
passwordValue = password.getText().toString(); if(userNameValue.equals("xuyinghui")&&passwordValue.equals(""))
{
Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
//登录成功和记住密码框为选中状态才保存用户信息
if(rem_pw.isChecked())
{
//记住用户名、密码、
Editor editor = sp.edit();
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD",passwordValue);
editor.commit();
}
//跳转界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
//finish(); }else{ Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
} }
}); //监听记住密码多选框按钮事件
rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (rem_pw.isChecked()) { System.out.println("记住密码已选中");
sp.edit().putBoolean("ISCHECK", true).commit(); }else { System.out.println("记住密码没有选中");
sp.edit().putBoolean("ISCHECK", false).commit(); } }
}); //监听自动登录多选框事件
auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (auto_login.isChecked()) {
System.out.println("自动登录已选中");
sp.edit().putBoolean("AUTO_ISCHECK", true).commit(); } else {
System.out.println("自动登录没有选中");
sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
}
}
}); btnQuit.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
finish();
}
}); }
}

其中在跳转页面时,增加了进度条。可以让用户知道程序还在进行中。避免纠结自己的程序是否终止。

2:页面布局login.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:background="@drawable/login2"
android:orientation="vertical" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/img_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/quit"/> <TextView
android:id="@+id/tv_zh"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:layout_marginLeft="12dip"
android:layout_marginTop="10dip"
android:gravity="bottom"
android:text="帐号:"
android:textColor="#000000"
android:textSize="18sp" /> <EditText
android:id="@+id/et_zh"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_below="@id/tv_zh"
android:layout_marginLeft="12dip"
android:layout_marginRight="10dip" /> <TextView
android:id="@+id/tv_mima"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:layout_below="@id/et_zh"
android:layout_marginLeft="12dip"
android:layout_marginTop="10dip"
android:gravity="bottom"
android:text="密码:"
android:textColor="#000000"
android:textSize="18sp" /> <EditText
android:id="@+id/et_mima"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_below="@id/tv_mima"
android:layout_marginLeft="12dip"
android:layout_marginRight="10dip"
android:maxLines=""
android:password="true"
android:scrollHorizontally="true" /> <CheckBox
android:id="@+id/cb_mima"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_mima"
android:layout_marginLeft="12dip"
android:text="记住密码"
android:textColor="#000000" /> <CheckBox
android:id="@+id/cb_auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cb_mima"
android:layout_marginLeft="12dip"
android:text="自动登录"
android:textColor="#000000" />
<Button
android:id="@+id/btn_login"
android:layout_width="80dip"
android:layout_height="40dip"
android:layout_below="@id/et_mima"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/cb_auto"
android:layout_marginRight="10dip"
android:gravity="center"
android:text="登录"
android:textColor="#000000"
android:textSize="18sp"/> </RelativeLayout> </LinearLayout>

3:页面显示如下:

4:源码下载请访问:https://github.com/xuyinghuicherish/SharedPreferences

关于SharedPreferences存储数据的使用方法的更多相关文章

  1. Android应用开发SharedPreferences存储数据的使用方法

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  2. 【Mark】Android应用开发SharedPreferences存储数据的使用方法

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  3. 【Android】数据的应用-使用sharedpreferences存储数据

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  4. android中使用SharedPreferences存储数据

    使用SharedPreferences存储数据还是比较简单的 1.添加或修改数据(没有数据就添加,有数据就是修改): SharedPreferences.Editor editor = getShar ...

  5. Android开发:SharedPreferences 存储数据、获取数据

    Android开发:SharedPreferences 存储数据.获取数据 email:chentravelling@163.com 开发环境:win7 64位,Android Studio. 关于S ...

  6. HTML5在客户端存储数据的新方法——localStorage

    HTML5在客户端存储数据的新方法--localStorage localStorage作为HTML5本地存储web storage特性的API之一,主要作用是将数据保存在客户端中,而客户端一般是指上 ...

  7. android开发之路11(用SharedPreferences存储数据)

    Android平台给我们提供了一个SharedPreferences类,实际上SharedPreferences处理的就是一个key-value(键值对),它是 一个轻量级的存储类,特别适合用于保存软 ...

  8. sharedPreferences存储数据

    sharedPreferences使用的是键值对的方式存储数据. 1.Android中三种获取sharedPreferences的方式 1)Context 类中的getSharedPreference ...

  9. [ Android 五种数据存储方式之一 ] —— SharedPreferences存储数据

    SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数. 主要是保存一些常用的配置比如窗口状态,一般在Activity中 重载窗口状态onSaveInstanceS ...

随机推荐

  1. 434. Number of Segments in a String

    原题: 434. Number of Segments in a String 解题: 刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况 思路: 一:遍历字符,if条件碰到非空格 ...

  2. oracle函数大全-字符处理函

    字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库类 ...

  3. centos6.5下oracle自动备份删除指定天数的文件

    第一步先做一个备份 #!/bin/sh export ORACLE_BASE=/home/oracle/app export ORACLE_HOME=/dbhome_1 export ORACLE_S ...

  4. 远程批量获取Linux和Windos操作系统版本(内核)

    在不登录远程主机的情况下,可以查看远程主机的服务器操作系统版本(内核). 脚本执行前提: 1.拷贝check_snmp到脚本执行的主机中或在此主机中安装nagios; 2.保持list.txt中只有一 ...

  5. Unity资源Assetbundle

    转  Unity资源打包之Assetbundle 本文原创版权归 csdn janeky 所有,转载请详细注明原创作者及出处,以示尊重! 作者:janeky 原文:http://blog.csdn.n ...

  6. node中可读流、可写流

    javascript的一个不足之处是不能处理二进制数据,于是node中引入了Buffer类型.这个类型以一个字节(即8位)为单位,给数据分配存储空间.它的使用类似于Array,但是与Array又有不同 ...

  7. MySql数据库常用语句汇总

    第一天1.登陆数据库 mysql -uroot -proot; //-u用户名 -p密码2.启动数据库 net start mysql;3.创建表空间(数据库)create database qy97 ...

  8. linux启动jmeter(二十三),执行./jmeter.sh报错解决方法(转载)

    转载自 http://www.cnblogs.com/yangxia-test 1.l-bash: ./jmeter.sh: Permission denied解决办法:jmeter.sh的执行权限改 ...

  9. centos 7 redis-4.0.11 主从

    redis-master:192.168.199.223 redis-slave: 192.168.199.224 cd /opt wget http://download.redis.io/rele ...

  10. openvpn-admin(openvpn web管理 )

    openvpn 两种认证简介: 1.key分发: 在服务器端生成秘钥,然后下载到本地,将服务器端的ca.crt xx.crt xx.key ta.key(如果服务器启用的话需要,未开启的话不需要,功能 ...