1.案例-找回密码-登录逻辑
1.1LoginMainActivity.java
package com.example.chapter05; 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.DialogInterface; import android.content.Intent; 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.chapter05.util.ViewUnil; import java.util.Random; public class LoginMainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener { private TextView tv_password; private EditText et_password; private Button btn_forget; private CheckBox ck_remember; private EditText et_phone; private RadioButton rb_verifycode; private RadioButton rb_password; private ActivityResultLauncher<Intent> register; private Button btn_login; private String mPassword = "111111"; private String mVerifyCode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_main); RadioGroup rg_login = findViewById(R.id.rg_login); tv_password = findViewById(R.id.tv_password); et_phone = findViewById(R.id.et_phone); 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); btn_login = findViewById(R.id.btn_login); // 给rg_login设置单选监听器 rg_login.setOnCheckedChangeListener(this); // 给et_phone添加文本变更监听器 et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11)); // 给et_password添加文本变更监听器 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"); } } }); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { // 选择了密码登录 case R.id.rb_password: tv_password.setText(getString(R.string.login_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 v) { String phone = et_phone.getText().toString(); if (phone.length() < 11) { Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); return; } switch(v.getId()) { case R.id.btn_forget: // 选择了密码方式校验,此时要跳到找回密码页面 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 (!mPassword.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("确定返回", (dialog, which) -> { // 结束当前的活动页面 finish(); }); builder.setNegativeButton("我再看看", null); AlertDialog dialog = builder.create(); dialog.show(); } // 定义一个编辑框监听器,在输入文本达到指定长度时自动隐藏输入法 private class HideTextWatcher implements TextWatcher { private EditText mView; private int maxLength; public HideTextWatcher(EditText v, int maxLength) { this.mView = v; this.maxLength = maxLength; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (s.toString().length() == maxLength) { // 隐藏输入法软键盘 ViewUnil.hideOneInputMethod(LoginMainActivity.this, mView); } } } }
1.2效果:
2.案例-找回密码-找回密码
2.1LoginForgetActivity.java
package com.example.chapter05; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.util.Random; public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener { private String mPhone; private String mVerifyCode; private EditText et_password_first; private EditText et_password_second; private EditText et_verifycode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_forget); et_password_first = findViewById(R.id.et_password_first); et_password_second = findViewById(R.id.et_password_second); et_verifycode = findViewById(R.id.et_verifycode); // 从上一个页面获取要修改密码的手机号码 mPhone = getIntent().getStringExtra("phone"); findViewById(R.id.btn_verifycode).setOnClickListener(this); findViewById(R.id.btn_confirm).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_verifycode: // 点击了“获取验证码”按钮 // 生成六位随机数字的验证码 mVerifyCode = String.format("%06d", new Random().nextInt(999999)); // 以下弹出提醒对话框,提示用户记住六位验证码数字 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请记住验证码"); builder.setMessage("手机号" + mPhone + ",本次验证码是" + mVerifyCode + ",请输入验证码"); builder.setPositiveButton("好的", null); AlertDialog dialog = builder.create(); dialog.show(); break; case R.id.btn_confirm: // 点击了“确定”按钮 String password_first = et_password_first.getText().toString(); String password_second = et_password_second.getText().toString(); if (password_first.length() < 6) { Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show(); return; } if (!password_first.equals(password_second)) { Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show(); return; } if (!mVerifyCode.equals(et_verifycode.getText().toString())) { Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show(); return; } Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show(); // 以下把修改好的新密码返回给上一个页面 Intent intent = new Intent(); intent.putExtra("new_password", password_first); setResult(Activity.RESULT_OK, intent); finish(); break; } } }
2.2效果:
标签:10,27,private,2022,import,et,android,password,id From: https://www.cnblogs.com/pingfanliliang/p/16830754.html