前言
大家好,我是oy,今天介绍一下在登录页面中如何实现自动登录及记住密码。
一、效果
二、设计思路
- 使用sharedpreferenced存储用户账号和密码,以及是否记住密码和自动登录。
- 记住密码和自动登录按钮都采用checkbutton,使用checkbutton的oncheckedchangelistener监听。
三、知识点介绍
sharedpreferenced
sharedpreferenced是android中存储数据的一种方式。采用键值对的方式存储数据。
使用过程:
- ① 获取sharedpreferenced对象。
- ② 调用edit()获取sharepreferenced.editor对象。
- ③ 调用putboolean()…等向sharepreferenced.editor对象添加数据。
- ④ 调用apply()提交数据。
例子
// 存数据 sharedpreferences sp = getsharedprefrences("data", mode_private);// 获取sharedpreferenced对象 sharedpreferences.editor ed = sp.edit();// 获取sharepreferenced.editor对象 ed.putstring("name", "sam");// 向sharepreferenced.editor对象添加数据 ed.apply();// 调用apply()提交数据,就是保存的意思 // 取数据 sharedprefrences sp = getsharedprefrences("data",mode_private); string name = sp.getstring("name","");// 取数据
checkbutton就不介绍了
四、自动登录及记住密码实现
分为两个activity,mainactivity是登录页面,homeactivity是登录成功页面。
homeactivity.java代码
public class homeactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); } }
activity_home.xml代码
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".homeactivity"> <textview android:id="@+id/tv_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text" android:textsize="26sp" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintend_toendof="parent" app:layout_constraintstart_tostartof="parent" app:layout_constrainttop_totopof="parent" /> </androidx.constraintlayout.widget.constraintlayout>
mainactivity.java代码
private appcompatedittext edit_account, edit_password; private checkbox cb_remember, cb_autologin; private sharedpreferences sharedpreferences; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); bindview(); initview(); } /** *用于绑定控件id的方法 */ protected void bindview() { edit_account = findviewbyid(r.id.edit_account); edit_password = findviewbyid(r.id.edit_password); cb_remember = findviewbyid(r.id.cb_remember); cb_remember.setoncheckedchangelistener(this); cb_autologin = findviewbyid(r.id.cb_autologin); cb_autologin.setoncheckedchangelistener(this); button btn_login = findviewbyid(r.id.btn_login); btn_login.setonclicklistener(this); // 获取sharedpreferences的实例 sharedpreferences = this.getsharedpreferences("logininfo", mode_private); } /** * 用于初始化界面 */ protected void initview() { // 获取sharedpreferences中remember对于的boolean值,true表示记住密码 if (sharedpreferences.getboolean("remember", false)) { cb_remember.setchecked(true); edit_account.settext(sharedpreferences.getstring("account", "")); edit_password.settext(sharedpreferences.getstring("password","")); autologin(); } } // 登录按钮的逻辑 @override public void onclick(view view) { // 定义账号和密码的字符串 string account, password; // 判断账号是否为空 if (edit_account.gettext() == null) { showtoast("账号为空,请重新输入"); return; } // 判断密码是否为空 if (edit_password.gettext() == null) { showtoast("密码为空,请重新输入"); return; } // 账号和密码都不为空,进行密码账号校验 account = edit_account.gettext().tostring().trim(); password = edit_password.gettext().tostring().trim(); // 此处固定了账号和密码 if (account.equals("admin") && password.equals("12345")) { if (cb_remember.ischecked()) { sharedpreferences.editor editor = sharedpreferences.edit(); editor.putstring("account", account); editor.putstring("password", password); editor.apply(); } showtoast("登录成功"); intent intent = new intent(mainactivity.this, homeactivity.class);// 跳转到主界面 startactivity(intent); // finish(); } } /** * 自动登录逻辑 */ private void autologin() { // 获取sharedpreferences中autologin对于的boolean值, true表示记住密码 if (sharedpreferences.getboolean("autologin", false)) { // 勾选自动登录 cb_autologin.setchecked(true); // 跳转页面 intent intent = new intent(mainactivity.this, homeactivity.class);// 跳转到主界面 startactivity(intent); } } /** * 用于显示toast弹出消息 * @param text 需要显示的文本 */ private void showtoast(final string text) { toast.maketext(mainactivity.this, text, toast.length_short).show(); } // checkbutton按钮的选中监听事件,compoundbutton指的是checkbutton控件, ischecked指的是是否勾选 @suppresslint("nonconstantresourceid") @override public void oncheckedchanged(compoundbutton compoundbutton, boolean ischecked) { switch (compoundbutton.getid()) { case r.id.cb_remember: if (ischecked) { sharedpreferences.edit().putboolean("remember", true).apply(); } else { sharedpreferences.edit().putboolean("remember", false).apply(); } break; case r.id.cb_autologin: if (ischecked) { sharedpreferences.edit().putboolean("autologin", true).apply(); } else { sharedpreferences.edit().putboolean("autologin", false).apply(); } break; } }
sharedpreferenced存储是位于data/data/包名/shared_prefs下。是xml文件存储键值对。
比如
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <boolean name="remember" value="true" /> <boolean name="autologin" value="true" /> <string name="password">12345</string> <string name="account">admin</string> </map>
总结与补充
主要介绍了在登录页面中如何实现自动登录和记住密码的功能,简单介绍了sharedpreferences的使用方法,这也是android中存储数据常用的方法之一。android存储还有sqlite数据库存储,在另一篇文章 " android studio登录注册的实现及介绍 " 中有讲到。
到此这篇关于android大作业功能设计之自动登录和记住密码的文章就介绍到这了,更多相关android自动登录和记住密码内容请搜索七九推以前的文章或继续浏览下面的相关文章希望大家以后多多支持七九推!
发表评论