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); } } }
作者:冰稀饭Aurora
出处:https://www.cnblogs.com/rsy-bxf150/p/17220539.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端