android数据存储心得,android学习心得 轻量级存储SharePreferences
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
下面通过“记住密码”功能
四、实例
效果图如下
首页
登录成功后的页面
当第一次登录点击”记住密码“后,第二次打开时的页面
2.代码
布局文件 login.xml
encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:layout_gravity="right"
android:background="@drawable/default_bg"
android:orientation="vertical">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
android:gravity="center"
android:layout_gravity="center">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ivlogo"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
android:layout_marginTop="100dip">
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">
android:layout_width="70px"
android:layout_height="wrap_content"
android:id="@+id/etaccount"
android:layout_marginRight="20dip"
android:maxLength="20">
android:layout_marginTop="10dip">
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">
android:layout_width="70px"
android:layout_height="wrap_content"
android:layout_marginRight="20dip"
android:id="@+id/etpw"
android:inputType="textPassword">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dip"
android:layout_marginRight="20dip">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvclear"
android:text="清除Cookies"
android:textColor="#aa0000"
android:textSize="12px">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip">
android:gravity="center"
android:layout_width="fill_parent">
android:layout_width="100px"
android:layout_height="wrap_content"
android:id="@+id/btnlogin"
android:layout_gravity="center"
android:text="登录">
android:layout_width="100px"
android:layout_height="wrap_content"
android:id="@+id/btnexit"
android:layout_gravity="center"
android:text="退出">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="25dip">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cbrp"
android:text="记住密码"
android:textSize="12px">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cbal"
android:text="自动登录"
android:textSize="12px">
java代码
package com.wjq;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
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.TextView;
import android.widget.Toast;
import com.wjq.beans.User;
import com.wjq.func.UserMgr;
public class Login 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;
private TextView tvClear;
@Override
protected void onCreate(Bundle
savedInstanceState) {
// TODO Auto-generated method
stub
super.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=(TextView)findViewById(R.id.tvclear);
InitConfig();
cbrp
.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public
void onCheckedChanged(CompoundButton buttonView,
boolean
isChecked) {
sp
= getSharedPreferences("UserInfo", 0);
sp.edit().putBoolean("cbrp",
isChecked).commit();
}
});
cbal
.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public
void onCheckedChanged(CompoundButton buttonView,
boolean
isChecked) {
sp
= getSharedPreferences("UserInfo", 0);
sp.edit().putBoolean("cbal",
isChecked).commit();
}
});
btnLogin.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View v) {
user
= new User(etAccount.getText().toString(),
etPW.getText()
.toString());
Log.i("tag",
"Account:" + etAccount.getText().toString());
Log.i("tag",
"Password:" + etPW.getText().toString());
userMgr
= new UserMgr();
Boolean
flag = userMgr.CheckUser(user, Login.this);
if
(!flag) {
Toast.makeText(Login.this,
"用户验证错误!", 1000).show();
}
else
{
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();
}
}
}
});
btnExit.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View v) {
System.exit(0);
}
});
tvClear.setOnClickListener(new
OnClickListener(){
@Override
public void
onClick(View v) {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));
}
}
说明:
1.写内容
sp = getSharedPreferences("UserInfo",
0);
sp.edit().putBoolean("cbal",
isChecked).commit();
UserInfo是指xml文件的文件名,如果此文件已存在则直接向其中写内容“isChecked”的值,首先通过SharedPreferences的edit()方法创建editor,然后调用commit()方法提修改
2.读内容
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));
android数据存储心得,android学习心得 轻量级存储SharePreferences相关推荐
- Android数据备份(Android Data Backup)
最近我在阅读Android Developer上的文章,本文是对其中一篇Data Backup的翻译.希望可以通过翻译英文技术文章提高自己阅读英文文档的水平,如果有不妥的地方,希望指出,谢谢~因为这个 ...
- php成绩查询系统的学习心得_PHP学习心得与体会
PHP 学习心得与体会 我想在讲述自己的学习方式前,对那些期望能从我的文 章中获得有用信息的人说一句心里话: 我的文章不会对您的学习起到实质性的作用,您能否成 功, 还得靠自己的,坚持,坚持,再坚持, ...
- 学数学计算机课的心得,课程学习心得体会
课程学习心得体会本人在高中数学新课程培训中认真听取专家讲课,对于新课标有一定的心得体会,现具体汇报如下.高中数学课程是义务教育或普通高级中学的一门主要课程,它从国际意识.时代需求.国民素质.个性发展的 ...
- html作业心得体会,学习心得体会范文6篇
以下是留学网提供的学习心得体会范文,想了解更多情况请关注出国留学网. 学习心得体会范文1: 学习就像一个无望无际的海洋,那样宽广,那样伟大,我们就像一艘小船,在这迷茫的大海中,寻找着彼岸,而上天对它的 ...
- android数据持久化框架,Android:数据持久化、Environment、SharedPreferences、内部存储internalStorage...
1.数据持久化-SDCard//数据持久化-SDCard findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @ ...
- android数据存放map_go语言学习笔记(18)-二维数组和map
二维数组 二维数组在声明/定义时有对应四种写法(类似一维数组) var 数组名 [大小][大小]类型 = [大小][大小]类型{{初值...},{初值...}} var 数组名 [大小][大小]类型 ...
- android 数据业务,【Android架构Telephony篇】之数据业务(一)
Android: 4.4.4 Desktop: Ubuntu 15.04 这里只做一些准备事情. 不得不说,Android里的Telephony模块还是挺复杂的,要想搞个八九分清楚需要花点功夫.今天把 ...
- android数据写入文件格式,Android 本地文件保存数据(2020-08-07)
文件操作需要手机权限,需要在AndroidManifest.xml添加 --------------------------------第一种方式--------------------------- ...
- 医用计算机学习心得,计算机学习心得体会范文
跟着信息技能的高速开展并疾速浸透到社会糊口的方方面面,较量争论机日趋成为人们进修.任务.糊口不成短少的根本东西,接上去就跟好范文网小编一同往理解一下对于较量争论机进修心患上领会范文吧! 较量争论机进修 ...
- 对oracle的心得体会,学习心得征文活动精选一:Oracle学习的心得体会
详情: 经常遇到朋友问oracle学习难不难,怎么才能成为高手等等,我想结合我的个人经验简单说几点: 1.打好基础,由浅入深 学习Oracle不能急于求成,寄希望于一天成为一个大侠.学习有个过程,应该 ...
最新文章
- 再见,Windows 7!盘点 2020 影响开发者的十大事件!
- 硬件知识:U盘缩水是怎么回事,如何恢复U盘真实容量?
- 2021年广东副高考试成绩查询,广东省中山市2021年高级会计师考试成绩可以查询了吗?...
- 【ElasticSearch】Es 源码之 GatewayMetaState 源码解读
- 使用 Visual Studio 2012进行C语言开发
- echarts版本升级导致Chinamap渲染报错问题(vue2)
- Big FAT32 Format Pro(U盘格式化工具)官方正式版V2.0 | u盘无法格式化怎么办 | 万能u盘修复工具下载 | 突破FAT文件系统4GB的限制
- IT售前工程师需要掌握哪些技术
- eMMC的使用寿命分析
- cogs——1008. 贪婪大陆(清华巨佬代码)——树状数组
- Java 正则表达式对用户名、手机号、邮箱等验证
- 数据结构-算法与算法描述
- 怎么理解——用户不是人
- java memorystream 包_C#到Java:Base64String,MemoryStream,GZipStream
- QString和QDateTime之间相互转化
- 2023重庆理工大学计算机考研信息汇总
- 抛开那些人生大道理,保持初心,砥砺前行
- 限定地区|媒体人赴世界名校伯克利大学自费访学
- 文件查看器示例支持 文件预览,编辑和视频播放
- redmine 表格_Redmine Custom tables 自定义表格插件 - Redmine插件中文站
热门文章
- mysql 列合并_mysql 列转行,合并字段的方法(必看)
- c语言 集中上机题目,C语言集中上机题目.doc
- 按钮 每一行_word跨页表格如何重复设置表头?单击“重复标题行”按钮来设置多页表格重复标题行显示。...
- 2.php函数,PHP常用函数总结(2)
- make: *** 没有规则可以创建“default”需要的目标“build”
- 用Python实现一个SVM分类器策略
- 再次记录 Visual Studio 2015 CTP 5 的一个坑
- 【Todo】各种语言里面的for循环 loop
- MessageBox、::MessageBox 、AfxMessageBox三者的区别
- UIView使用UIMotionEffect效果