Android用SharedPreferences实现登录注册注销功能
前言
本文用SharedPreferences本地缓存账号信息来实现登录注册功能,以及退出注销功能。
一、本文逻辑
本文的注册登录逻辑如下:
- 注册页面:有账号可以直接去登录页面。没有账号的话填写账号密码,检测账号密码不为空,点击立即注册,保存账号信息,跳转到登录页面。
- 登录页面:首先读取缓存的账号密码和是否记住密码,将账号显示,判断记住密码的标志,为空或false,不显示密码,需要输入密码,为true则直接将缓存的密码显示,选择是否记住密码按钮,将此状态存入缓存,点击登录跳转到主页。
- 主页:首先取缓存,判断是否登录,若没有登录跳转到注册页面。点击退出登录,跳转到注册页;(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"/>
<</

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

1044

被折叠的 条评论
为什么被折叠?



