Android学习 | 09.优化记住密码

本文介绍了一个Android登录界面的实现,焦点转移时自动填充数据库中记住的密码。当手机号存在且选择了记住密码,焦点移到密码框时会自动填入密码;否则清空。使用SQLite数据库存储登录信息,并提供了查询、保存和更新数据的函数。
摘要由CSDN通过智能技术生成

这次要实现的效果是,输入手机号码,如果数据库中有该手机号对应的密码并且选择了记住密码,则当焦点转移到密码框的时候自动填入密码。如果没有对应密码或者没有选择记住密码,则焦点转移到密码框的时候清空输入框。

视图:activity_login_sqlite.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RadioGroup
        android:id="@+id/rg_login"
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rb_password"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="true"
            android:text="@string/login_by_password"
            android:textSize="@dimen/common_font_size"
            android:textColor="@color/black"/>
        <RadioButton
            android:id="@+id/rb_verifycode"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/login_by_verifycode"
            android:textSize="@dimen/common_font_size"
            android:textColor="@color/black"/>
    </RadioGroup>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/phone_number"
            android:gravity="center"
            android:textSize="@dimen/common_font_size"
            android:textColor="@color/black" />
        <EditText
            android:id="@+id/et_phone"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/editext_selector"
            android:hint="please input phone number"
            android:inputType="number"
            android:maxLength="11"
            android:textColor="@color/black"
            android:textColorHint="@color/grey"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/password"
            android:gravity="center"
            android:textSize="@dimen/common_font_size"
            android:textColor="@color/black" />
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >
            <EditText
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_weight="1"
                android:background="@drawable/editext_selector"
                android:hint="@string/input_password"
                android:inputType="numberPassword"
                android:maxLength="6"
                android:textColor="@color/black"
                android:textColorHint="@color/grey"
                android:textSize="@dimen/common_font_size" />
            <Button
                android:id="@+id/btn_forget"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:text="@string/forget_password"
                android:textSize="@dimen/common_font_size"
                android:textColor="@color/black"/>
        </RelativeLayout>
    </LinearLayout>
    <CheckBox
        android:id="@+id/ck_remember"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/remember_password"
        android:textSize="@dimen/common_font_size"
        android:textColor="@color/black"/>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/common_font_size"
        android:textColor="@color/white"
        android:text="@string/login" />

</LinearLayout>

对应java文件:LoginSQLiteActivity.java

package com.example.chapter06;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chapter06.database.LoginDBHelper;
import com.example.chapter06.enity.LoginInfo;
import com.example.chapter06.util.ViewUtil;

import java.util.Random;

public class LoginSQLiteActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener, View.OnFocusChangeListener {

    private RadioGroup rg_login;
    private TextView tv_password;
    private EditText et_password;
    private Button btn_forget;
    private CheckBox ck_remember;
    private EditText et_phone;
    private RadioButton rb_password;
    private RadioButton rb_verifycode;
    private ActivityResultLauncher<Intent> register;
    private Button btn_login;
    private String mPassword = "111111";
    private String mVerifyCode;
    private SharedPreferences preferences;
    private LoginDBHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_main);

        rg_login = findViewById(R.id.rg_login);
        tv_password = findViewById(R.id.tv_password);
        et_password = findViewById(R.id.et_password);
        btn_forget = findViewById(R.id.btn_forget);
        ck_remember = findViewById(R.id.ck_remember);
        rb_password = findViewById(R.id.rb_password);
        rb_verifycode = findViewById(R.id.rb_verifycode);
        et_phone = findViewById(R.id.et_phone);
        btn_login = findViewById(R.id.btn_login);
        // 设置单选监听器
        rg_login.setOnCheckedChangeListener(this);
        // 添加文本变更监听器
        et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11));
        et_password.addTextChangedListener(new HideTextWatcher(et_password, 6));

        btn_forget.setOnClickListener(this);
        btn_login.setOnClickListener(this);

        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                Intent intent = result.getData();
                if (intent != null && result.getResultCode() == Activity.RESULT_OK) {
                    mPassword = intent.getStringExtra("new_password");
                }
            }
        });

        et_password.setOnFocusChangeListener(this);
    }

    private void reload() {
        LoginInfo info = mHelper.quertTop();
        if (info != null && info.remember) {
            et_phone.setText(info.phone);
            et_password.setText(info.password);
            ck_remember.setChecked(true);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mHelper = LoginDBHelper.getInstance(this);
        mHelper.openReadLink();
        mHelper.openWriteLink();

        reload();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mHelper.closeLink();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_password:
                tv_password.setText(getString(R.string.password));
                et_password.setHint(getString(R.string.input_password));
                btn_forget.setText(getString(R.string.forget_password));
                ck_remember.setVisibility(View.VISIBLE);
                break;
            case R.id.rb_verifycode:
                tv_password.setText(getString(R.string.verifycode));
                et_password.setHint(getString(R.string.input_verifycode));
                btn_forget.setText(getString(R.string.get_verifycode));
                ck_remember.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        String phone = et_phone.getText().toString();
        switch (view.getId()) {
            case R.id.btn_forget:
                if (phone.length() < 11) {
                    Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                //选择了密码校验方式,此时要跳转到找回密码页面
                if (rb_password.isChecked()) {
                    //
                    Intent intent = new Intent(this, LoginForgetActivity.class);
                    intent.putExtra("phone", phone);
                    register.launch(intent);
                } else if (rb_verifycode.isChecked()) {
                    // 生成六位随机数
                    mVerifyCode = String.format("%06d", new Random().nextInt(999999));
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("请记住验证码");
                    builder.setMessage("手机号" + phone + ",本次验证码是 " + mVerifyCode + ",请输入验证码");
                    builder.setPositiveButton("好的", null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                break;
            case R.id.btn_login:
                // 密码方式校验
                if (rb_password.isChecked()) {
                    if (!mPassword.equals(et_password.getText().toString())) {
                        Toast.makeText(this,"请输入正确的密码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    // 提示用户登录成功
                    loginSuccess();
                }else if (rb_verifycode.isChecked()) {
                    // 验证码方式校验
                    if (!mVerifyCode.equals(et_password.getText().toString())) {
                        Toast.makeText(this,"请输入正确的验证码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    loginSuccess();
                }
                break;
        }
    }

    // 校验通过,登录成功
    private void loginSuccess() {
        String desc = String.format("您的手机号是%s,恭喜你通过登录验证,点击“确定”按钮返回上一个页面",
                et_phone.getText().toString());
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("登录成功");
        builder.setMessage(desc);
        builder.setPositiveButton("确定返回", (dialogInterface, i) -> {
            finish();
        });
        builder.setNegativeButton("我再看看", null);
        AlertDialog dialog = builder.create();
        dialog.show();

        // 保存到数据库
        LoginInfo info = new LoginInfo();
        info.phone = et_phone.getText().toString();
        info.password = et_password.getText().toString();
        info.remember = ck_remember.isChecked();
        mHelper.save(info);
    }

    // 当密码输入框获取焦点之后,根据输入的电话号码,查询出对应的密码,自动填入
    @Override
    public void onFocusChange(View view, boolean b) {
        if (view.getId() == R.id.et_password && b) {
            LoginInfo info = mHelper.queryByPhone(et_phone.getText().toString());
            // 如果根据电话号码,查询出了密码
            if (info != null) {
                et_password.setText(info.password);
                ck_remember.setChecked(info.remember);
            }else {
                // 没有查到,清空密码
                et_password.setText("");
                ck_remember.setChecked(false);
            }
        }
    }

    // 定义一个编辑框监听器,输入文本达到指定长度的时候自动隐藏输入法
    private class HideTextWatcher implements TextWatcher {
        private EditText mView;
        private int mMaxLength;

        public HideTextWatcher(EditText v, int maxLength) {
            this.mView = v;
            this.mMaxLength = maxLength;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() == mMaxLength) {
                ViewUtil.hideOneInputMethod(LoginSQLiteActivity.this,mView);
            }
        }
    }
}

大体和上一个差不多,改变的地方有:

  • 添加了onFocusChange方法,当密码输入框获取焦点之后,根据输入的电话号码,查询出对应的密码,自动填入
  • reload方法将最后一条数据放到第一个显示
  • 将每次输入的信息保存到数据库

数据库帮助器:LoginDBHelper.java

package com.example.chapter06.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.chapter06.enity.LoginInfo;
import com.example.chapter06.enity.User;

import java.util.ArrayList;
import java.util.List;

public class LoginDBHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "user.db";
    private static final String TABLE_NAME = "user_info";
    private static final int DB_VERSION = 2;
    private static LoginDBHelper mHelper = null;
    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;

    private LoginDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    //利用单例模式获取数据库帮助器的唯一实例
    public static LoginDBHelper getInstance(Context context) {
        if (mHelper == null) {
            mHelper = new LoginDBHelper(context);
        }
        return mHelper;
    }

    // 打开数据库的读连接
    public SQLiteDatabase openReadLink() {
        if (mWDB ==null || !mWDB.isOpen()) {
            mWDB = mHelper.getReadableDatabase();
        }
        return mWDB;
    }

    // 打开数据库的写连接
    public SQLiteDatabase openWriteLink() {
        if (mRDB ==null || !mRDB.isOpen()) {
            mRDB = mHelper.getWritableDatabase();
        }
        return mRDB;
    }

    //关闭数据库连接
    public void closeLink() {
        if (mRDB != null && mRDB.isOpen()) {
            mRDB.close();
            mRDB = null;
        }

        if (mWDB != null && mWDB.isOpen()) {
            mWDB.close();
            mWDB = null;
        }
    }

    // 创建数据库,执行建表语句
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +" (" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                "phone VARCHAR NOT NULL," +
                "password INTEGER NOT NULL," +
                "remember INTEGER NOT NULL);";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    }

    public void save(LoginInfo info) {
        // 如果存在,则先删除后添加
        try {
            mWDB.beginTransaction();
            delete(info);
            insert(info);
            mWDB.setTransactionSuccessful();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            mWDB.endTransaction();
        }
    }

    public long delete(LoginInfo info) {
        return mWDB.delete(TABLE_NAME,"phone=?",new String[]{info.phone});
    }

    public long insert (LoginInfo info) {
        ContentValues values = new ContentValues();
        values.put("phone",info.phone);
        values.put("password",info.password);
        values.put("remember",info.remember);
        return mWDB.insert(TABLE_NAME,null,values);
    }

    public long deleteByName(String name) {
        // 删除所有
        // mWDB.delete(TABLE_NAME,"1=1", null);
        return mWDB.delete(TABLE_NAME, "name=?", new String[]{name});
    }

    public List<User> quertByName(String name) {
        List<User> list = new ArrayList<>();
        // 执行记录查询动作,该语句返回结果集的游标
        Cursor cursor = mRDB.query(TABLE_NAME, null, "name=?", new String[]{name}, null, null, null);
        // 循环取出游标指向的每条记录
        while (cursor.moveToNext()) {
            User user = new User();
            user.id = cursor.getInt(0);
            user.name = cursor.getString(1);
            user.age = cursor.getInt(2);
            user.height = cursor.getLong(3);
            user.weight = cursor.getFloat(4);
            // SQLite没有布尔类型,用0表示false,用1表示true
            user.married = (cursor.getInt(5) == 0) ? false : true;
            list.add(user);
        }
        return list;
    }

    public LoginInfo quertTop() {
        LoginInfo info = null;
        String sql = " select * from " + TABLE_NAME + " where remember = 1 ORDER BY _id DESC limit 1 ";
        // 执行记录查询动作,该语句返回结果集的游标
        Cursor cursor = mRDB.rawQuery(sql, null);
        // 循环取出游标指向的每条记录
        if (cursor.moveToNext()) {
            info = new LoginInfo();
            info.id = cursor.getInt(0);
            info.phone = cursor.getString(1);
            info.password = cursor.getString(2);
            // SQLite没有布尔类型,用0表示false,用1表示true
            info.remember = (cursor.getInt(3) == 0) ? false : true;
        }
        return info;
    }

    public LoginInfo queryByPhone(String phone) {
        LoginInfo info = null;
        String sql = " select * from " + TABLE_NAME + " where remember = 1 ORDER BY _id DESC limit 1 ";
        // 执行记录查询动作,该语句返回结果集的游标
        Cursor cursor = mRDB.query(TABLE_NAME, null, "phone=? and remember = 1", new String[]{phone},null,null,null);
        // 循环取出游标指向的每条记录
        if (cursor.moveToNext()) {
            info = new LoginInfo();
            info.id = cursor.getInt(0);
            info.phone = cursor.getString(1);
            info.password = cursor.getString(2);
            // SQLite没有布尔类型,用0表示false,用1表示true
            info.remember = (cursor.getInt(3) == 0) ? false : true;
        }
        return info;
    }
}

不同的地方有:

  • 修改了建表sql语句
  • 修改了save方法,使得如果存在该手机号则先删除后添加
  • 添加了一个queryTop方法,查询remember=1(记住密码)的数据,用if(cursor.moveToNext())条件循环取出游标指向的每条记录
  • 添加了一个queryByPhone方法,通过手机号搜索对应数据

登录信息类:LoginInfo.java

定义了登录所需信息

package com.example.chapter06.enity;

public class LoginInfo {
    public int id;
    public String phone;
    public String password;
    public boolean remember = false;

    public LoginInfo() {}

    public LoginInfo(String phone, String password, boolean remember) {
        this.phone = phone;
        this.password = password;
        this.remember = remember;
    }

    @Override
    public String toString() {
        return "LoginInfo{" +
                "id=" + id +
                ", phone='" + phone + '\'' +
                ", password='" + password + '\'' +
                ", remember=" + remember +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值