服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android通过"记住密码"功能学习数据存储类SharedPreferences详解及实例

Android通过"记住密码"功能学习数据存储类SharedPreferences详解及实例

2021-06-23 15:50ForrestWoo Android

这篇文章主要通过“记住密码”实例功能学习为大家介绍了Android数据存储类SharedPreferences,感兴趣的小伙伴们可以参考一下

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

下面通过“记住密码”功能

四、实例

效果图如下

Android通过"记住密码"功能学习数据存储类SharedPreferences详解及实例

首页

 Android通过"记住密码"功能学习数据存储类SharedPreferences详解及实例

登录成功后的页面

 Android通过"记住密码"功能学习数据存储类SharedPreferences详解及实例

当第一次登录点击”记住密码“后,第二次打开时的页面

2.代码

布局文件 login.xml

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?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:gravity="right" android:layout_gravity="right"
 android:background="@drawable/default_bg" android:orientation="vertical">
 <tablelayout android:layout_width="fill_parent"
 android:layout_height="wrap_content" android:stretchcolumns="1">
 <tablerow android:gravity="center" android:layout_gravity="center">
 <imageview android:layout_width="fill_parent"
 android:layout_height="wrap_content" android:id="@+id/ivlogo"
 >
 </imageview>
 </tablerow>
 </tablelayout>
 <tablelayout android:layout_width="fill_parent"
 android:layout_height="wrap_content" android:stretchcolumns="1">
 <tablerow android:layout_margintop="100dip">
 <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">
 </textview>
 
 <edittext android:layout_width="70px" android:layout_height="wrap_content"
 android:id="@+id/etaccount" android:layout_marginright="20dip"
 android:maxlength="20"></edittext>
 </tablerow>
 <tablerow android:layout_margintop="10dip">
 <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">
 </textview>
 
 <edittext android:layout_width="70px" android:layout_height="wrap_content"
 android:layout_marginright="20dip" android:id="@+id/etpw"
 android:inputtype="textpassword"></edittext>
 </tablerow>
 </tablelayout>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:orientation="horizontal" android:layout_margintop="5dip" android:layout_marginright="20dip">
 
 <textview android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:id="@+id/tvclear"
 android:text="清除cookies" android:textcolor="#aa0000" android:textsize="12px"></textview>
 
 </linearlayout>
 <tablelayout android:layout_width="fill_parent"
 android:layout_height="wrap_content" android:layout_margintop="20dip">
 <tablerow android:gravity="center" android:layout_width="fill_parent">
 <button android:layout_width="100px" android:layout_height="wrap_content"
 android:id="@+id/btnlogin" android:layout_gravity="center"
 android:text="登录"></button>
 
 <button android:layout_width="100px" android:layout_height="wrap_content"
 android:id="@+id/btnexit" android:layout_gravity="center"
 android:text="退出"></button>
 </tablerow>
 </tablelayout>
 
 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 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>
 <checkbox android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:id="@+id/cbal"
 android:text="自动登录" android:textsize="12px"></checkbox>
 </linearlayout>
</linearlayout>

java代码

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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;
 
 /*
 * (non-javadoc)
 *
 * @see android.app.activity#oncreate(android.os.bundle)
 */
 @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.写内容

?
1
2
3
sp = getsharedpreferences("userinfo", 0);
 sp.edit().putboolean("cbal", ischecked).commit();
 userinfo是指xml文件的文件名,如果此文件已存在则直接向其中写内容“ischecked”的值,首先通过sharedpreferences的edit()方法创建editor,然后调用commit()方法提修改

2.读内容

?
1
2
3
4
5
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));

以上就是本文的全部内容,希望对大家的学习有所帮助。

延伸 · 阅读

精彩推荐