关于SharedPreferences存储数据的使用方法
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存储数据的使用方法的更多相关文章
- Android应用开发SharedPreferences存储数据的使用方法
Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...
- 【Mark】Android应用开发SharedPreferences存储数据的使用方法
Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...
- 【Android】数据的应用-使用sharedpreferences存储数据
Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...
- android中使用SharedPreferences存储数据
使用SharedPreferences存储数据还是比较简单的 1.添加或修改数据(没有数据就添加,有数据就是修改): SharedPreferences.Editor editor = getShar ...
- Android开发:SharedPreferences 存储数据、获取数据
Android开发:SharedPreferences 存储数据.获取数据 email:chentravelling@163.com 开发环境:win7 64位,Android Studio. 关于S ...
- HTML5在客户端存储数据的新方法——localStorage
HTML5在客户端存储数据的新方法--localStorage localStorage作为HTML5本地存储web storage特性的API之一,主要作用是将数据保存在客户端中,而客户端一般是指上 ...
- android开发之路11(用SharedPreferences存储数据)
Android平台给我们提供了一个SharedPreferences类,实际上SharedPreferences处理的就是一个key-value(键值对),它是 一个轻量级的存储类,特别适合用于保存软 ...
- sharedPreferences存储数据
sharedPreferences使用的是键值对的方式存储数据. 1.Android中三种获取sharedPreferences的方式 1)Context 类中的getSharedPreference ...
- [ Android 五种数据存储方式之一 ] —— SharedPreferences存储数据
SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数. 主要是保存一些常用的配置比如窗口状态,一般在Activity中 重载窗口状态onSaveInstanceS ...
随机推荐
- 使用xshell xftp连接centos的过程。
1 配置网络 登录Centtos系统,配置网卡. cd 空格/etc/sysconfig/network-scripts 回车键. ls 查看文件 vi ifcig-eth0 编辑这个文件 修改为 ...
- js版RSA算法
// RSA, a suite of routines for performing RSA public-key computations in// JavaScript.//// Requires ...
- Hibernate学习笔记2.4(Hibernate的Id生成策略)
通过设置告诉id该怎么设置. 1.通过xml方式 1.assigned 主键由外部程序负责生成,在 save() 之前必须指定一个.Hibernate不负责维护主键生成.与Hibernate和底层数据 ...
- editable : false与 readonly 的区别
editable : false 不能输入 readonly:不可操作,只能看
- Windows Server 2008 R2微软官方下载
注意:Windows Server 2008 R2仅有64位版本. 以下下载地址为Windows Server 2008 R2 RTM Build 7600.16385的评估版本,此版本可免费试用18 ...
- 主流浏览器 Cookie 的大小
目前测试的两种浏览器:Chrome 和 IE . 环境:IIS 7.5 只创建了站点,未做任何配置. Chrome : v36.0.1985.143 m 单个 Cookie 的长度为 4069 个字符 ...
- js 中的原型prototype
每次创建新函数,就会根据规则为该函数创建一个 prototype 属性,该属性是一个指向函数原型对象的指针.并且原型对象都默认拥有一个 constructor 属性,该属性是一个指向那个新建函数的指针 ...
- sql 允许远程登录
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option; flush privileges ...
- JPA in
CriteriaBuilder.In in = criteriaBuilder.in(root.get("field1")); for (String str : arr) { i ...
- vmware 共享文件夹
参考 https://jingyan.baidu.com/article/7f766daf7866be4101e1d0ed.html 只是设置共享文件夹选项还不行,需要从安装vmware的安装路径中找 ...