Android用SharedPreferences实现登录注册注销功能

前言

本文用SharedPreferences本地缓存账号信息来实现登录注册功能,以及退出注销功能。


一、本文逻辑

本文的注册登录逻辑如下:

  1. 注册页面:有账号可以直接去登录页面。没有账号的话填写账号密码,检测账号密码不为空,点击立即注册,保存账号信息,跳转到登录页面。
  2. 登录页面:首先读取缓存的账号密码和是否记住密码,将账号显示,判断记住密码的标志,为空或false,不显示密码,需要输入密码,为true则直接将缓存的密码显示,选择是否记住密码按钮,将此状态存入缓存,点击登录跳转到主页。
  3. 主页:首先取缓存,判断是否登录,若没有登录跳转到注册页面。点击退出登录,跳转到注册页;(2)点击注销账号,清除所有缓存,跳转到注册页面。

二、代码

1.布局界面

注册页面 activity_register.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:background="#AB2196F3"android:gravity="center"android:text="用户注册"android:textSize="20dp"android:textStyle="bold" /></RelativeLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginRight="10dp"android:layout_marginLeft="10dp"android:layout_marginTop="20dp"android:orientation="vertical"><EditTextandroid:id="@+id/reg_uname"android:layout_marginTop="40dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:hint="帐号"/><EditTextandroid:id="@+id/reg_pwd"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:inputType="textPassword"android:hint="密码"/><EditTextandroid:id="@+id/reg_pwd2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:inputType="textPassword"android:hint="再次确认密码"/><Buttonandroid:id="@+id/register_btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="#AB2196F3"android:text="立即注册"/><Buttonandroid:id="@+id/register_toLogin_btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="#AB2196F3"android:text="已有账号?去登录"/></LinearLayout></LinearLayout>

登录界面 activity_login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:background="#AB2196F3"android:gravity="center"android:text="用户登录"android:textSize="20dp"android:textStyle="bold" /></RelativeLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="20dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:orientation="vertical"><EditTextandroid:id="@+id/login_username"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:hint="账号"/><EditTextandroid:id="@+id/login_password"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="密码"android:inputType="textPassword"android:layout_margin="10dp"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"><CheckBoxandroid:id="@+id/login_remember"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="记住密码" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:clickable="true"android:onClick="register"android:textColor="#3F51B5"android:text="没有账号?立即注册" /></RelativeLayout><Buttonandroid:id="@+id/login_btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#AB2196F3"android:text="登录"android:layout_margin="10dp"/></LinearLayout>
</LinearLayout>

主页面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"android:orientation="vertical"android:padding="15dp"tools:context=".MainActivity"><TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="主页!"android:textSize="30dp"android:layout_centerHorizontal="true"android:layout_alignParentTop="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="主页内容!"android:textSize="30dp"android:layout_marginTop="50dp"android:layout_below="@id/text"android:layout_centerHorizontal="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom"android:text="退出登录"android:layout_alignParentBottom="true"android:onClick="Quit"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom"android:text="注销此账号"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:onClick="zhuxiao"/></RelativeLayout>

2.Activity 逻辑

注册页面的逻辑

package com.example.yuan;import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;/*** Created by Yuan on 2020/8/6 17:08.* 包名: com.example.yuan* 类说明:注册(按钮的点击事件直接使用 Activity 作为事件监听器)*/
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {//获取用户名,昵称,密码,确认密码private EditText reg_uname,reg_uname2,et_pwd,et_pwd2;//获取用户名,昵称,密码,确认密码的值private String uname,pwd,pwd2;private SharedPreferences sp;private SharedPreferences.Editor editor;
private Button btn_reg,btn_toLogin;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register);init();//获取界面控件}//UI组件初始化与事件绑定private void init(){//UI组件初始化reg_uname=findViewById(R.id.reg_uname);et_pwd=findViewById(R.id.reg_pwd);et_pwd2=findViewById(R.id.reg_pwd2);btn_toLogin=findViewById(R.id.register_toLogin_btn);btn_reg=findViewById(R.id.register_btn);//点击事件绑定btn_reg.setOnClickListener(this);btn_toLogin.setOnClickListener(this);}//点击事件@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.register_btn://获取参数uname=reg_uname.getText().toString().trim();pwd=et_pwd.getText().toString().trim();pwd2=et_pwd2.getText().toString().trim();//判断if(uname.equals("") || pwd.equals("") ){Toast.makeText(RegisterActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();return;}else if(!(pwd.equals(pwd2))){Toast.makeText(RegisterActivity.this,"两次输入密码不一致",Toast.LENGTH_SHORT).show();}else {//loginInfo表示文件名sp=getSharedPreferences("loginInfo", MODE_PRIVATE);//获取编辑器editor=sp.edit();//存入注册的用户信息editor.putString("username", uname);editor.putString("password",pwd);//提交修改editor.commit();Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();startActivity(new Intent(getApplicationContext(),LoginActivity.class));}break;case R.id.register_toLogin_btn://跳转到登录页面startActivity(new Intent(getApplicationContext(),LoginActivity.class));finish();break;}}
}

登录页面的逻辑

package com.example.yuan;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;/*** Created by Yuan on 2020/8/6 16:12.* 包名: com.example.yuan* 类说明:登录*/
public class LoginActivity extends AppCompatActivity {private EditText et_uname,et_upwd;//获取用户名,密码private CheckBox isRemember;private Button login_btn;private String username,password,sp_name,sp_pwd;private Boolean sp_isRemember;//是否记住密码的标志private Context mContext;private SharedPreferences sp;private SharedPreferences.Editor editor;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);init();//获取界面控件//读取缓存数据sp=getSharedPreferences("loginInfo", MODE_PRIVATE);sp_name=sp.getString("username","");sp_pwd=sp.getString("password","");sp_isRemember=sp.getBoolean("isRemember",false);//如果有账号,直接显示Log.d("TAG",sp_name);et_uname.setText(sp_name);//如果上次登录记住了密码,本次登录直接设置上if(sp_isRemember.equals(true)){et_upwd.setText(sp_pwd);isRemember.setChecked(true);}mContext= LoginActivity.this;//登录按钮的点击事件(直接用匿名内部类)login_btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//开始登录,获取输入的用户名和密码username = et_uname.getText().toString().trim();password = et_upwd.getText().toString().trim();// TextUtils.isEmpty判断是否输入if (TextUtils.isEmpty(username)) {Toast.makeText(mContext, "请输入手机号", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(password)) {Toast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT).show();// 判断,输入的账号密码,是否与保存在SharedPreferences中一致} else if (username.equals(sp_name) && password.equals(sp_pwd)) {//一致登录成功Toast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT).show();//判断记住密码按钮是否选中,选中则将isRemember设置为true,保存状态sp=getSharedPreferences("loginInfo", MODE_PRIVATE);//获取编辑器editor=sp.edit();if (isRemember.isChecked()) {//如果记住密码选中,存入true,否则存入falseeditor.putBoolean("isRemember",true);}else {editor.putBoolean("isRemember",false);}editor.putBoolean("isLogin",true);//保存登录状态editor.apply();//提交修改//登录成功后关闭此页面进入主页//跳转到主界面,登录成功的状态传递到 MainActivity 中Intent intent=new Intent(getApplicationContext(),MainActivity.class);intent.putExtra("name",username);startActivity(intent);finish();//销毁登录界面} else if ((sp_pwd != null && !TextUtils.isEmpty(sp_pwd) && !password.equals(sp_pwd))) {Toast.makeText(mContext, "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show();} else {Toast.makeText(mContext, "此用户名不存在", Toast.LENGTH_SHORT).show();}}});}//获取界面控件private void init(){et_uname=findViewById(R.id.login_username);et_upwd=findViewById(R.id.login_password);isRemember=findViewById(R.id.login_remember);login_btn=findViewById(R.id.login_btn);}
//注册按钮的点击事件(直接绑定在控件上)public void register(View view) {Intent intent=new Intent(getApplicationContext(),RegisterActivity.class);startActivity(intent);}
}

主页面的逻辑

package com.example.yuan;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {private  SharedPreferences sp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView textView=findViewById(R.id.text);Intent intent=getIntent();String name=intent.getStringExtra("name");sp=getSharedPreferences("loginInfo", MODE_PRIVATE);textView.setText("欢迎你,"+name);Boolean isLogin=sp.getBoolean("isLogin",false);if (!isLogin){startActivity(new Intent(getApplicationContext(),RegisterActivity.class));finish();}else if(TextUtils.isEmpty(name)){startActivity(new Intent(getApplicationContext(),LoginActivity.class));finish();}}public void Quit(View view) {sp=getSharedPreferences("loginInfo", MODE_PRIVATE);SharedPreferences.Editor editor=sp.edit();editor.putBoolean("isLogin",false);editor.commit();startActivity(new Intent(getApplicationContext(),RegisterActivity.class));finish();}/*** @param view*/public void zhuxiao(View view) {sp=getSharedPreferences("loginInfo", MODE_PRIVATE);SharedPreferences.Editor editor=sp.edit();editor.clear();editor.commit();Toast.makeText(MainActivity.this,"注销成功",Toast.LENGTH_SHORT).show();startActivity(new Intent(getApplicationContext(),RegisterActivity.class));finish();}}

总结

在android 中存储数据时经常用SharedPreference, 并且在提交数据时一直用的是Editor的commit方法, 也有用apply方法的。

apply和commit方法的不同:

apply没有返回值而commit返回boolean表明修改是否提交成功
apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步的提交到硬件磁盘,因此,在多个并发的提交commit的时候,他们会等待正在处理的commit保存到磁盘后在操作,从而降低了效率。而apply只是原子的提交到内容,后面有调用apply的函数的将会直接覆盖前面的内存数据,这样从一定程度上提高了很多效率。
apply方法不会提示任何失败的提示。

由于在一个进程中,sharedPreference是单实例,一般不会出现并发冲突,如果对提交的结果不关心的话,建议使用apply,当然需要确保提交成功且有后续操作的话,还是需要用commit的。

Android 实现 登录注册注销功能相关推荐

  1. node.js+vue.js+mysql实现登录注册的功能(前后端分离)

    参考教程:github源码地址 Node.js+Mysql+Vue+ElementUI 实现登录注册注销功能 1 准备 1.1 MySQL数据库 参考教程:NodeJS连接MySql.易百教程.菜鸟教 ...

  2. android studio使用Bmob来实现登录注册的功能

    刚刚抛下了Eclipse,初学了android studio,一开始真是用起来感觉难受,不过的确能够实现一些Eclipse不能实现的功能,而且用起来还蛮方便. 网上虽然有很多关于这等方面的教程,但是一 ...

  3. Android用户登录注册界面

    用户登录注册界面开发及用户信息管理案例详解 刚开始接触Android编程,这算是我写的第一个简单工程,主要功能有:用户登录.注册.注销.修改密码.记住密码共5个基本操作,其内容涉及到以下几点: 1:B ...

  4. django 登录注册注销

    一.设计数据模型 1.数据库模型设计 作为一个用户登录和注册项目,需要保存的都是各种用户的相关信息.很显然,我们至少需要一张用户表User,在用户表里需要保存下面的信息: 用户名 密码 邮箱地址 性别 ...

  5. Android开发 登录注册设计

    用Android Studio 简单的实现登录注册 目录 一.登录界面 activity_login.xml 布局代码: login.java 代码: 二.注册界面 activity_register ...

  6. Android之登录注册——简易版

    今天,我要分享给大家的是Android中常见的一个的登录注册的案例,我这里写的是简易版,如果大家有更精彩的拓展,可以自行发挥哦! 运行过程相信大家都已经心知肚明了,所以我在这里就直接发布代码了,其中有 ...

  7. 分享发现一个优秀WP插件 2022最新WordPress登录注册会员功能一体的插件LOGINUSER-CH

    手机/QQ登录注册完美代替传统登录注册会员中心,建站必备独立用户中心,功能完整(支持投稿),让你博客提升一个档次,如需充值搭配市面ErphpDown插件效果更佳.手机+社会化登录 代替早该淘汰传统邮箱 ...

  8. uniapp实现微信手机号码登录注册注销(bug踩坑)

    @toc 本文是使用手机授权uniapp登录 解決办法 (主要解决方法,就是code和解析phone分开处理=) 第一步:处理code onload就开始un.login 方法通过code调用后台接口 ...

  9. Android - 原生登录注册页面【仿】淘宝App

    bean文件夹:解析数据 LoginBean.java private String msg;private String code;private DataBean data;public Stri ...

最新文章

  1. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
  2. 弹性分组环(RPR)技术特点及其在城域网中的应用
  3. 计算未来轻沙龙 | AI=知识+推理,知识工程与数据管理专场来了!
  4. jinja Evnironment
  5. 《Power Designer系统分析与建模实战》——1.4 本章小结
  6. 单片机00:继电器间隔1s的控制
  7. 高质量的原创对于搜索引擎来说是非常的友好的
  8. vue 移动端H5微信支付和支付宝支付
  9. arcgis flex api 由于沙箱安全的原因 无法显示地图 的解决办法
  10. 还在对比IT培训机构,行内人告诉你怎么比?
  11. [漏洞分析] CVE-2022-0847 Dirty Pipe linux内核提权分析
  12. 美少年为“苹果”编程挣钱 12岁办软件开发公司
  13. debussy和nlint常用快捷键
  14. LearningSpark(5):Spark共享变量理解
  15. 一站式在线订货功能详解,B2B电子商务交易平台高效解决企业订单管理痛点
  16. 操作系统真象还原——第6章 完善内核
  17. TikTok代运营丨跨境卖家怎么玩TikTok?
  18. Windows调试工具入门 — windebug
  19. Android实现箭头无限循环上升的简单动画
  20. 美国加州中学课本 教材介绍 - Glencoe系列第一辑 - 介绍

热门文章

  1. java String转数组||String转集合||将字符串转化为数组
  2. 杰理之mesh组网测试demo【篇】
  3. 移动互联网的未来:金钱跟着眼球走
  4. 小米平板6.0以上系统如何不用Root激活Xposed框架的步骤
  5. 【Rcode】生存分析: KM COX回归 随机森林 nomogram
  6. 穿越派·派盘+Joplin=私有云盘
  7. 江西师范大学计算机考研真题,江西师范大学计算机信息工程学院数据结构与程序设计历年考研真题大全附答案.docx...
  8. oracle 给表指定表空间,oracle数据库创建用户指定表空间
  9. jquery iframe自适应高度
  10. Linux-如何正确清理大文件