(1)声明一个共享参数对象,并在onCreate函数中调用getSharedPreferences方法获取共享参数的实例。
(2)登录成功时,如果用户勾选了“记住密码”,就使用共享参数保存手机号码与密码。
(3)再次打开登录页面时,App从共享参数中读取手机号码与密码,并展示在界面上。
利用SharePreferences将已经登陆成功的账号密码进行数据存储if (ck_remember.isChecked()){SharedPreferences.Editor edtor=remember.edit();edtor.putString("phonenum",et_phone.getText().toString());edtor.putString("password",et_password.getText().toString());mit();}
对SharePreference中的数据进行读取public void reload(){boolean isremember=remember.getBoolean("isremember",false);if(isremember){String phone=remember.getString("phonenum","");String password=remember.getString("password","");et_phone.setText(phone);et_password.setText(password);}}
// 下面是利用共享参数保存密码的代码
// 如果勾选了“记住密码”,则把手机号码和密码都保存到共享参数中
if (bRemember) {
SharedPreferences.Editor editor = mShared.edit(); // 获得编辑器的对象editor.putString("phone", et_phone.getText().toString()); // 添加名叫phone的手机号码editor.putString("password", et_password.getText().toString()); // 添加名叫password的密码mit(); // 提交编辑器中的修改 } // 下面是利用共享参数读取密码的代码 // 从share.xml中获取共享参数对象
mShared = getSharedPreferences("share_login", MODE_PRIVATE);
// 获取共享参数中保存的手机号码
String phone = mShared.getString("phone", ""); // 获取共享参数中保存的密码
String password = mShared.getString("password", ""); et_phone.setText(phone);
// 给手机号码编辑框填写上次保存的手机号
et_password.setText(password); // 给密码编辑框填写上次保存的密码
package com.example.chapter05;import android.annotation.SuppressLint;import android.app.AlertDialog;import android.content.DialogInterface;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 poundButton;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.Toast;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.AppCompatActivity;import com.example.chapter05.util.ViewUtil;import java.util.Random;@SuppressLint("DefaultLocale")public class LoginMainActivity extends AppCompatActivity implements View.OnClickListener {private RadioGroup rg_login; // 声明一个单选组对象private RadioButton rb_password; // 声明一个单选按钮对象private RadioButton rb_verifycode; // 声明一个单选按钮对象private EditText et_phone; // 声明一个编辑框对象private TextView tv_password; // 声明一个文本视图对象private EditText et_password; // 声明一个编辑框对象private Button btn_forget; // 声明一个按钮控件对象private CheckBox ck_remember; // 声明一个复选框对象private int mRequestCode = 0; // 跳转页面时的请求代码private boolean bRemember = false; // 是否记住密码private String mPassword = "111111"; // 默认密码private String mVerifyCode; // 验证码private ActivityResultLauncher<Intent> register;private SharedPreferences remember;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login_main);rg_login = findViewById(R.id.rg_login);rb_password = findViewById(R.id.rb_password);rb_verifycode = findViewById(R.id.rb_verifycode);et_phone = findViewById(R.id.et_phone);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);// 给rg_login设置单选监听器rg_login.setOnCheckedChangeListener(new RadioListener());// 给ck_remember设置勾选监听器ck_remember.setOnCheckedChangeListener(new CheckListener());// 给et_phone添加文本变更监听器et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11));// 给et_password添加文本变更监听器et_password.addTextChangedListener(new HideTextWatcher(et_password, 6));btn_forget.setOnClickListener(this);findViewById(R.id.btn_login).setOnClickListener(this);register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {@Overridepublic void onActivityResult(ActivityResult result) {}});remember = getSharedPreferences("remember", MODE_PRIVATE);reload();}// 定义登录方式的单选监听器private class RadioListener implements RadioGroup.OnCheckedChangeListener {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if (checkedId == R.id.rb_password) { // 选择了密码登录tv_password.setText("登录密码:");et_password.setHint("请输入密码");btn_forget.setText("忘记密码");ck_remember.setVisibility(View.VISIBLE);} else if (checkedId == R.id.rb_verifycode) { // 选择了验证码登录tv_password.setText("验证码:");et_password.setHint("请输入验证码");btn_forget.setText("获取验证码");ck_remember.setVisibility(View.INVISIBLE);}}}// 定义是否记住密码的勾选监听器private class CheckListener implements CompoundButton.OnCheckedChangeListener {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (buttonView.getId() == R.id.ck_remember) {bRemember = isChecked;}}}// 定义一个编辑框监听器,在输入文本达到指定长度时自动隐藏输入法private class HideTextWatcher implements TextWatcher {private EditText mView; // 声明一个编辑框对象private int mMaxLength; // 声明一个最大长度变量public HideTextWatcher(EditText v, int maxLength) {super();mView = v;mMaxLength = maxLength;}// 在编辑框的输入文本变化前触发public void beforeTextChanged(CharSequence s, int start, int count, int after) {}// 在编辑框的输入文本变化时触发public void onTextChanged(CharSequence s, int start, int before, int count) {}// 在编辑框的输入文本变化后触发public void afterTextChanged(Editable s) {String str = s.toString(); // 获得已输入的文本字符串// 输入文本达到11位(如手机号码),或者达到6位(如登录密码)时关闭输入法if ((str.length() == 11 && mMaxLength == 11)|| (str.length() == 6 && mMaxLength == 6)) {ViewUtil.hideOneInputMethod(LoginMainActivity.this, mView); // 隐藏输入法软键盘}}}@Overridepublic void onClick(View v) {String phone = et_phone.getText().toString();if (v.getId() == R.id.btn_forget) { // 点击了“忘记密码”按钮if (phone.length() < 11) { // 手机号码不足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);//startActivityForResult(intent, mRequestCode); // 携带意图返回上一个页面} 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 alert = builder.create();alert.show(); // 显示提醒对话框}} else if (v.getId() == R.id.btn_login) { // 点击了“登录”按钮if (phone.length() < 11) { // 手机号码不足11位Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();return;}if (rb_password.isChecked()) { // 密码方式校验if (!et_password.getText().toString().equals(mPassword)) {Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();} else { // 密码校验通过loginSuccess(); // 提示用户登录成功}} else if (rb_verifycode.isChecked()) { // 验证码方式校验if (!et_password.getText().toString().equals(mVerifyCode)) {Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();} else { // 验证码校验通过loginSuccess(); // 提示用户登录成功}}}}// 从下一个页面携带参数返回当前页面时触发@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == mRequestCode && data != null) {// 用户密码已改为新密码,故更新密码变量mPassword = data.getStringExtra("new_password");}}// 从修改密码页面返回登录页面,要清空密码的输入框@Overrideprotected void onRestart() {super.onRestart();et_password.setText("");}// 校验通过,登录成功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("确定返回", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {finish(); // 结束当前的活动页面}});builder.setNegativeButton("我再看看", null);AlertDialog alert = builder.create();alert.show(); // 显示提醒对话框if (ck_remember.isChecked()){SharedPreferences.Editor edtor=remember.edit();edtor.putString("phonenum",et_phone.getText().toString());edtor.putString("password",et_password.getText().toString());edtor.putBoolean("isremember",ck_remember.isChecked());mit();}}public void reload(){boolean isremember=remember.getBoolean("isremember",false);if(isremember){String phone=remember.getString("phonenum","");String password=remember.getString("password","");et_phone.setText(phone);et_password.setText(password);}}}
如果觉得《android:登录界面记住密码》对你有帮助,请点赞、收藏,并留下你的观点哦!