Android 实现 登录注册注销功能

本文详细介绍如何使用SharedPreferences在Android应用中实现用户登录、注册及注销功能,包括界面设计、逻辑处理及数据存储。

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">

    <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"/>
        <</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值