Android登陆界面的记住密码功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android登陆界面的记住密码功能

沙小光   2022-06-03 我要评论

所需工具

一、复选框控件:CheckBox,
二、SharedPreferences用于存储数据,该工具的读取和写入较为简单,放在代码里的注释中解释

实现逻辑:

如果没弄懂逻辑,代码看起来还是有点小难度的

一、判断SharedPreferences中已存入的CheckBox的Boolean信息(没有读取到则默认条件为“否”),如果条件为“是”(同时满足能读取到和读取的信息为“是”两个条件),通过SharedPreferences将存储的数据(account和password)读取出来并写入对应的文本框。

二、点击登录按键时,判断CheckBox是否勾选,如果条件为“是”,则将accout和password框里的数据(String)以及CheckBox的数据(Boolean)写入SharedPreferences,若没有勾选,则清除SharedPreferences中的数据。

实现代码

一、ui界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="60dp">
    <TextView
        android:layout_width="90dp"
        android:text="Account"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_gravity="center"
        />
    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:id="@+id/account"/>

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp">
    <TextView
        android:layout_width="90dp"
        android:text="Password"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_gravity="center"/>
    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:id="@+id/password"/>
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/remember_pass"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="Rember password"/>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="LogIn"
        android:id="@+id/login"/>
</LinearLayout>

二、实现功能部分

package com.example.broadcastbestpractice;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends BaseActivity {
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    private SharedPreferences pref;//通过pref读取SharedPreferences的数据
    private SharedPreferences.Editor editor;//editor将数据写入SharedPreferences
    private CheckBox rememberPass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        pref= PreferenceManager.getDefaultSharedPreferences(this);
        accountEdit = (EditText) findViewById(R.id.account);
        passwordEdit = (EditText) findViewById(R.id.password);
        rememberPass=(CheckBox)findViewById(R.id.remember_pass);
        login = (Button) findViewById(R.id.login);
        boolean isRemenber=pref.getBoolean("remember_password",false);//读取上次登陆时存入"remember_password"的信息,没有读取到则默认为false
        if(isRemenber)//如果读取为true,则将account和password,checkbox的信息写入文本框
        {
            String account=pref.getString("account","");
            String password=pref.getString("password","");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            rememberPass.setChecked(true);
        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String accout = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                if (accout.equals("1") && password.equals("1")) {
                    editor=pref.edit();
                    if(rememberPass.isChecked()){//如果勾选了checkbox框,则将account,password,checkbox信息写入
                        editor.putBoolean("remember_password",true);
                        editor.putString("account",accout);
                        editor.putString("password",password);
                    }else {
                        editor.clear();//若没有,清除SharedPreferences存储的信息
                    }
                    editor.apply();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else
                    Toast.makeText(LoginActivity.this, "account or password is wrong", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

功能完善

我在app内部添加了一个强制下线功能,这样,进入app后点击下线按钮就能直接退出到登陆界面,会使效果更加直观。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们