一、界面设计
直接贴代码吧
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/item_layout_height"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/input_new_password"
android:textColor="@color/black"
android:textSize="@dimen/common_font_size" />
<EditText
android:id="@+id/et_password_first"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="@drawable/editext_selector"
android:hint="@string/input_new_password_hint"
android:inputType="numberPassword"
android:maxLength="11"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textSize="@dimen/common_font_size" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/item_layout_height"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/confirm_new_password"
android:textColor="@color/black"
android:textSize="@dimen/common_font_size" />
<EditText
android:id="@+id/et_password_second"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="@drawable/editext_selector"
android:hint="@string/input_new_password_again"
android:inputType="numberPassword"
android:maxLength="11"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textSize="@dimen/common_font_size" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/item_layout_height"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/verifycode2"
android:textColor="@color/black"
android:textSize="@dimen/common_font_size" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:id="@+id/et_verifycode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="@drawable/editext_selector"
android:hint="@string/input_verifycode"
android:inputType="numberPassword"
android:maxLength="11"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textSize="@dimen/common_font_size" />
<Button
android:id="@+id/btn_verifycode"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:text="@string/get_verifycode"
android:textColor="@color/black"
android:textSize="@dimen/common_font_size" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="@+id/btn_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/done"
android:textColor="@color/black"
android:textSize="@dimen/button_font_size" />
</LinearLayout>
二、逻辑完善
直接看注释
public void onClick(View v) {
if(v == btn_verifycode){ // 如果点击的是验证码按钮
// 生成一个随机的6位验证码
mverifycode = String.format("%06d", new Random().nextInt(999999));
// 创建一个AlertDialog来显示验证码
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请记住验证码"); // 设置对话框标题
// 设置对话框消息,包括手机号和生成的验证码
builder.setMessage("手机号" + mphone + ",本次验证码是" + mverifycode + ",请输入验证码");
builder.setPositiveButton("好的", null); // 设置一个没有操作的确定按钮
AlertDialog dialog = builder.create(); // 创建AlertDialog
dialog.show(); // 显示AlertDialog
} else if (v == btn_confirm) { // 如果点击的是确认按钮
// 从EditText字段获取输入的密码
String first = et_password_first.getText().toString();
String second = et_password_second.getText().toString();
if(first.length() < 6){ // 检查密码长度是否小于6个字符
// 显示一个提示消息指示输入正确的密码长度
Toast.makeText(this, "请输入正确的密码长度", Toast.LENGTH_SHORT).show();
return; // 退出方法
}
if(!first.equals(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("first", first); // 将第一个密码放入intent中
setResult(Activity.RESULT_OK, intent); // 将结果设置为OK,并将intent传递过去
finish(); // 结束当前活动
}
}