今天来让昨天写的前端动起来
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
private TextView tv_password;
private Button btn_forget;
private Button btn_login;
private EditText et_password;
private EditText et_phone;
private CheckBox ck_remember;
private RadioButton rb_password;
private RadioButton rb_verifycode;
private ActivityResultLauncher<Intent> register;
private String mPassword = "878787";
private String verifycode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
RadioGroup rg_login = findViewById(R.id.rg_login);
rg_login.setOnCheckedChangeListener(this);
tv_password = findViewById(R.id.tv_password);
btn_forget = findViewById(R.id.btn_forget);
btn_login = findViewById(R.id.btn_login);
btn_forget.setOnClickListener(this);
btn_login.setOnClickListener(this);
et_password = findViewById(R.id.et_password);
et_phone = findViewById(R.id.et_phone);
et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 10));
et_password.addTextChangedListener(new HideTextWatcher(et_password, 6));
ck_remember = findViewById(R.id.ck_remember);
rb_password = findViewById(R.id.rb_password);
rb_verifycode = findViewById(R.id.rb_verifycode);
register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult o) {
}
});
}
//切换密码和验证码
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(i == 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);
}else{
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);
}
}
//判断使用何种方式登入 切换忘记密码或是获取验证码
@Override
public void onClick(View view) {
String phone = et_phone.getText().toString();
if(phone.length() <10){
Toast.makeText(this,"请输入正确的手机号码",Toast.LENGTH_SHORT).show();
return;
}
if(view.getId() == 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()) {
verifycode = String.format("%06d",new Random().nextInt(999999));
AlertDialog.Builder builder = new AlertDialog.Builder((this));
builder.setTitle("请记住验证码");
builder.setMessage("手机号"+phone+"验证码:"+ verifycode +",请输入验证码");
builder.setPositiveButton("好的",null);
AlertDialog dialog = builder.create();
dialog.show();
}
} else if (view.getId() == 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(!verifycode.equals(et_password.getText().toString())){
Toast.makeText(this,"请输入正确的验证码",Toast.LENGTH_SHORT).show();
return;
}
loginSuccess();
}
}
}
//登入成功!!
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();
}
//自动隐藏键盘
private class HideTextWatcher implements TextWatcher {
private EditText mView;
private int mMaxlength;
public HideTextWatcher(EditText et, int maxlengh) {
this.mView = et;
this.mMaxlength = maxlengh;
}
@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 editable) {
if(editable.toString().length() == mMaxlength){
ViewUtil.hiadeOneInputMethod(MainActivity.this, mView);
}
}
}
}
今天先到这边另外找回密码的页面 等我做完作业再来拔:l