Loading

Android实现记住用户名和密码-CheckBox,SharedPreference

Android实现记住用户名和密码

先前我的打卡APP并没有记住登录者账号密码的功能,今天把这个功能实现了。

当我们勾选“记住密码”,退出应用后会保存账号密码,反之则会被清空

具体是使用了SharedPreference 存储来实现的。我们需要创建一个复选按钮,通过按钮的否选取来判断是否保存账号密码。

首先我们需要在xml中添加一个CheckBox复选框;

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cb_save1"
        android:text="记住密码"
        android:textColor="@color/white_text"
        android:textSize="18sp"
        android:layout_below="@+id/et_log_password"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="15dp" />

 

 在MainActivity中添加如下语句:

首先声明一个Activity中的变量,创建一个SharedPreferences,同时声明 CheckBox和 两个私有变量username,password用来表示账号密码

SharedPreferences sp = null;
private CheckBox cb_save1;
private String username,password;

在OnCreate中加入:

cb_save1 = (CheckBox) findViewById(R.id.cb_save1);

    if (sp.getBoolean("CheckBoxLogin", false)) {
            et_log_id.setText(sp.getString("username", null));
            et_log_password.setText(sp.getString("password", null));
            cb_save1.setChecked(true);
        }else {
            et_log_id.setText(sp.getString("username", username));
            et_log_password.setText(sp.getString("password", password));
            cb_save1.setChecked(true);
        }

检测复选框是否被选中,若未选中,则清空输入框,反之保存数据,默认复选框是选中的

在OnClick中加入:

    username=eText_id.getText().toString();
        password=eText_password.getText().toString();

在OnClick登录部分中加入:

            boolean CheckBoxLogin = cb_save1.isChecked();
                    SharedPreferences.Editor editor = sp.edit();
                    if (CheckBoxLogin) {
                        editor.putString("username", username);
                        editor.putString("password", password);
                        editor.putBoolean("auto", true);
                    }else {
                        editor.putString("username", null);
                        editor.putString("password", null);
                        editor.putBoolean("auto", false);
                    }
                    editor.commit();

 

完整代码

package com.example.clockappliction;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.clockappliction.DataBase.CRUD;
import com.example.clockappliction.Information.Student;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_login, btn_reg;

    private EditText et_log_id,et_log_password;
    private CheckBox cb_save1;
    private String username,password;

    SharedPreferences sp = null;

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

        sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);

        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);

        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_reg.setOnClickListener(this);

        et_log_id = (EditText) findViewById(R.id.et_log_id);
        et_log_password = (EditText) findViewById(R.id.et_log_password);

        cb_save1 = (CheckBox) findViewById(R.id.cb_save1);

        if (sp.getBoolean("CheckBoxLogin", false)) {
            et_log_id.setText(sp.getString("username", null));
            et_log_password.setText(sp.getString("password", null));
            cb_save1.setChecked(true);
        }else {
            et_log_id.setText(sp.getString("username", username));
            et_log_password.setText(sp.getString("password", password));
            cb_save1.setChecked(true);
        }


    }

    @Override
    public void onClick(View view) {

        username=et_log_id.getText().toString();
        password=et_log_password.getText().toString();


        if (view == findViewById(R.id.btn_login)){

            Student student = new Student();
            CRUD crud = new CRUD(this);
            //获取输入框的学生信息
            student.id = et_log_id.getText().toString();
            student.password = et_log_password.getText().toString();
            //通过id获取数据库学生信息
            Student studentData =crud.getStudentById(student.id);
            //登录
            if (student.id.equals(studentData.id)){

                if (student.password.equals(studentData.password)){

                    boolean CheckBoxLogin = cb_save1.isChecked();
                    SharedPreferences.Editor editor = sp.edit();
                    if (CheckBoxLogin) {
                        editor.putString("username", username);
                        editor.putString("password", password);
                        editor.putBoolean("auto", true);
                    }else {
                        editor.putString("username", null);
                        editor.putString("password", null);
                        editor.putBoolean("auto", false);
                    }
                    editor.commit();

                    student.name= studentData.name;
                    Intent intent =new Intent(this,MenuActivity.class);
                    intent.putExtra("st_id",student.id);
                    intent.putExtra("st_name",student.name);
                    startActivity(intent);
                }else {
                    Toast.makeText(this, "密码不正确", Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(this, "学号不正确", Toast.LENGTH_SHORT).show();
            }

        } else if (view == findViewById(R.id.btn_reg)) {
            //跳转到注册页面
            Intent intent = new Intent(this,RegActivity.class);
            intent.putExtra("keys",0);
            startActivity(intent);
        }


    }
    
}

 

posted @ 2023-03-15 22:52  冰稀饭Aurora  阅读(300)  评论(0编辑  收藏  举报