Android studio密码登录验证码登录(三)找回密码

 

一、界面设计 

直接贴代码吧

<?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();  // 结束当前活动
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值