广告
返回顶部
首页 > 资讯 > 精选 >Android用SharedPreferences怎么实现登录注册注销功能
  • 346
分享到

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

2023-06-30 10:06:39 346人浏览 安东尼
摘要

这篇“Android用SharedPreferences怎么实现登录注册注销功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇

这篇“Android用SharedPreferences怎么实现登录注册注销功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Android用SharedPreferences怎么实现登录注册注销功能”文章吧。

一、本文逻辑

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

1、注册页面:有账号可以直接去登录页面。没有账号的话填写账号密码,检测账号密码不为空,点击立即注册,保存账号信息,跳转到登录页面。

2、登录页面:首先读取缓存的账号密码和是否记住密码,将账号显示,判断记住密码的标志,为空或false,不显示密码,需要输入密码,为true则直接将缓存的密码显示,选择是否记住密码按钮,将此状态存入缓存,点击登录跳转到主页。

3、主页:首先取缓存,判断是否登录,若没有登录跳转到注册页面。点击退出登录,跳转到注册页;(2)点击注销账号,清除所有缓存,跳转到注册页面。

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

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

二、代码

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">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_width="match_parent"            android:layout_height="50dp"            android:background="#AB2196F3"            android:gravity="center"            android:text="用户注册"            android:textSize="20dp"            android:textStyle="bold" />    </RelativeLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginRight="10dp"        android:layout_marginLeft="10dp"        android:layout_marginTop="20dp"        android:orientation="vertical">        <EditText            android:id="@+id/reg_uname"            android:layout_marginTop="40dp"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="10dp"            android:hint="帐号"/>        <EditText            android:id="@+id/reg_pwd"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="10dp"            android:inputType="textPassWord"            android:hint="密码"/>        <EditText            android:id="@+id/reg_pwd2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="10dp"            android:inputType="textPassword"            android:hint="再次确认密码"/>        <Button            android:id="@+id/register_btn"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="10dp"            android:background="#AB2196F3"            android:text="立即注册"/>        <Button            android: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">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_width="match_parent"            android:layout_height="50dp"            android:background="#AB2196F3"            android:gravity="center"            android:text="用户登录"            android:textSize="20dp"            android:textStyle="bold" />    </RelativeLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginTop="20dp"        android:layout_marginLeft="10dp"        android:layout_marginRight="10dp"        android:orientation="vertical">        <EditText            android:id="@+id/login_username"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="10dp"            android:hint="账号"/>        <EditText            android:id="@+id/login_password"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="密码"            android:inputType="textPassword"            android:layout_margin="10dp"/>        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="10dp">            <CheckBox                android:id="@+id/login_remember"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="记住密码" />            <TextView                android: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>        <Button            android: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">    <TextView        android: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"/>    <TextView        android: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" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="bottom"        android:text="退出登录"        android:layout_alignParentBottom="true"        android:onClick="Quit"/>    <Button        android: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;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;    @Override    protected 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);    }//点击事件    @Override    public 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;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;    @Override    protected 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() {            @Override            public 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,否则存入false                        editor.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;    @Override    protected 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();    }        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用SharedPreferences怎么实现登录注册注销功能”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网精选频道。

--结束END--

本文标题: Android用SharedPreferences怎么实现登录注册注销功能

本文链接: https://www.lsjlt.com/news/327988.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android用SharedPreferences实现登录注册注销功能
    Android用SharedPreferences实现登录注册注销功能 前言 本文用SharedPreferences本地缓存账号信息来实现登录注册功能,以及退出注销功能。 一、本文...
    99+
    2022-11-13
  • Android用SharedPreferences怎么实现登录注册注销功能
    这篇“Android用SharedPreferences怎么实现登录注册注销功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇...
    99+
    2023-06-30
  • Android实现登录注册功能
    本文实例为大家分享了Android实现登录注册功能的具体代码,供大家参考,具体内容如下 运行环境 Android Studio 总体效果图 一、 设计注册页面的布局 二、完成注册...
    99+
    2022-11-13
  • android登录注册功能如何实现
    要实现Android的登录注册功能,你可以按照以下步骤进行操作:1. 创建一个布局文件来设计登录和注册界面。可以使用EditText...
    99+
    2023-10-20
    android
  • Android实现登录注册功能封装
    我们都知道Android应用软件基本上都会用到登录注册功能,那么对一个一个好的登录注册模块进行封装就势在必行了。这里给大家介绍一下我的第一个项目中所用到的登录注册功能的,已经对...
    99+
    2022-06-06
    封装 Android
  • Android使用http实现注册登录功能
    在项目中实现注册登录有很多种方式,一般对于初学者来说,不使用框架,采用http的post和get请求后台服务器,是一种更好理解底层源码的方式。使用框架实现注册登录虽然比自己封装pos...
    99+
    2022-11-13
  • Android中怎么利用MVP实现登录注册功能
    这篇文章给大家介绍Android中怎么利用MVP实现登录注册功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。model包:import com.bwei.mvps.bean.UserBean;public&...
    99+
    2023-05-30
    android mvp
  • Android中怎么利用OKhttp3实现登录注册功能
    这期内容当中小编将会给大家带来有关Android中怎么利用OKhttp3实现登录注册功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。一、Android前端实现新建一个login的项目,主要的几个文件在这...
    99+
    2023-06-20
  • Android实现登录界面的注册功能
    本文实例为大家分享了Android登录界面的注册实现代码,供大家参考,具体内容如下 注册一个登录界面在控制台将输入的信息文本选框展示出来 xml界面设计(前面已发) <xml ...
    99+
    2022-11-13
  • Android基于Sqlite怎么实现注册和登录功能
    本篇内容主要讲解“Android基于Sqlite怎么实现注册和登录功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android基于Sqlite怎么实现注册和登录功能”吧!实现逻辑项目的图片结...
    99+
    2023-06-30
  • Python实现注册登录功能
    用Python写个注册登录功能,供大家参考,具体内容如下 本文是用Python写一个注册登录功能,难度不大,很适合练手主要就是用列表和字典,以及逻辑判断用到的第3方库模块是time模...
    99+
    2022-11-13
  • Node.js实现登录注册功能
    本文实例为大家分享了Node.js实现登录注册功能的具体代码,供大家参考,具体内容如下 目录结构 注册页面: reg.html <!DOCTYPE html> <...
    99+
    2022-11-13
  • 怎么用python实现登录与注册功能
    本篇内容主要讲解“怎么用python实现登录与注册功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用python实现登录与注册功能”吧!1. 案例介绍本例设计一个用户登录和注册模块,使用 ...
    99+
    2023-06-26
  • jsp怎么实现登录和注册功能
    要实现登录和注册功能,可以按照以下步骤进行:1. 创建一个登录页面(login.jsp)和一个注册页面(register.jsp)。...
    99+
    2023-08-09
    jsp
  • NodeJs+MySQL怎么实现注册登录功能
    这篇文章主要介绍“NodeJs+MySQL怎么实现注册登录功能”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“NodeJs+MySQL怎么实现注册登录功能”文章能帮助大家解决问题。nodejs中mys...
    99+
    2023-06-30
  • Android基于Sqlite实现注册和登录功能
    Android中基于Sqlite实现注册和登录功能,供大家参考,具体内容如下 前言 写这篇博客主要是为了巩固一下学的Sqlite知识以及梳理一下这个项目的逻辑 实现逻辑 项目的图片...
    99+
    2022-11-13
  • QT实现用户登录注册功能
    本文实例为大家分享了QT实现用户登录注册的具体代码,供大家参考,具体内容如下 1、login.h #ifndef LOGIN_H #define LOGIN_H #include ...
    99+
    2022-11-13
  • python实现登录与注册功能
    本文实例为大家分享了python实现登录与注册的具体代码,供大家参考,具体内容如下 1. 案例介绍 本例设计一个用户登录和注册模块,使用 Tkinter 框架构建界面,主要用到画布、...
    99+
    2022-11-12
  • NodeJs+MySQL实现注册登录功能
    本文实例为大家分享了NodeJs+MySQL实现注册登录功能的具体代码,供大家参考,具体内容如下 之前写过一个没有连接数据库的注册与登陆的实现,这次加上了数据库 刚刚接触后端,很多不...
    99+
    2022-11-13
  • node.js怎么实现网站登录注册功能
    这篇文章主要介绍了node.js怎么实现网站登录注册功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇node.js怎么实现网站登录注册功能文章都会有所收获,下面我们一起来看看吧。效果如下  ...
    99+
    2023-06-17
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作