案例:找回密码

LoginMainActivity.java

package com.tiger.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.tiger.chapter05.util.ViewUtil;

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_password;
    private RadioButton rb_verifycode;
    private Button btn_login;
    private ActivityResultLauncher<Intent> register;

    private String verifyCode;

    private String mPassword = "123456";

    @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_password = findViewById(R.id.et_password);
        btn_forget = findViewById(R.id.btn_forget);
        ck_remember = findViewById(R.id.ck_remember);
        et_phone = findViewById(R.id.et_phone);
        rb_password = findViewById(R.id.rb_password);
        rb_verifycode = findViewById(R.id.rb_verifycode);
        btn_login = findViewById(R.id.btn_login);
        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {

                Intent data = result.getData();
                if (data != null && result.getResultCode() == Activity.RESULT_OK) {
                    String newPassword = data.getStringExtra("new_password");
                  //用户密码已更改为新密码,故更新密码变量
                    mPassword =newPassword;
                }

            }
        });

        //给rg_login 设置单选监听器
        rg_login.setOnCheckedChangeListener(this);

        //主对象引用被垃圾回收,那么主对象里的子引用也会被垃圾回收

        //给et_phone添加文本变更监听器
        et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11));
        et_password.addTextChangedListener(new HideTextWatcher(et_password, 6));
        btn_forget.setOnClickListener(this);

        btn_login.setOnClickListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == 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 v) {
        String phone = et_phone.getText().toString();
        if (phone.length() < 11) {
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_LONG).show();
            return;
        }

        if (R.id.btn_forget == v.getId()) {

            if (rb_password.isChecked()) {
                //携带手机号码跳转到找回密码页面
                Intent intent = new Intent(this, LoginForgetActivity.class);
                intent.putExtra("phone", phone);
                register.launch(intent);
            } else {
                //生成6位随机的验证码
                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 alertDialog = builder.create();
                alertDialog.show();
            }


        } else if (R.id.btn_login == v.getId()) {

            if (rb_password.isChecked()) {
                if (!mPassword.equals(et_password.getText().toString())) {
                    Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_LONG).show();
                    return;
                }

                //提示用户登录成功
                loginSuccess();


            } else {

                if ("".equals(verifyCode)) {
                    Toast.makeText(this, "请先获取验证码", Toast.LENGTH_LONG).show();
                    return;
                }

                if (!verifyCode.equals(et_password.getText().toString())) {
                    Toast.makeText(this, "验证码不正确", Toast.LENGTH_LONG).show();
                    return;
                }


                //提示用户登录成功
                loginSuccess();

                verifyCode = "";


            }


        }


    }

    private void loginSuccess() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("登录成功");
        builder.setMessage("手机号" + et_phone.getText().toString() + "登录成功了");
        builder.setPositiveButton("确定返回", (dialog, which) -> {
            finish();
        });
        builder.setNegativeButton("我在看看", null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    //定义一个编辑框监听器 在输入文本达到指定长度是自动隐藏输入法
    private class HideTextWatcher implements TextWatcher {

        private EditText mView;
        private int maxLength;

        public HideTextWatcher(EditText mView, int maxLength) {
            this.mView = mView;
            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) {

            //获得已输入的文本字符串
            String str = s.toString();

            //输入文本达到11位 (如手机号码),或者达到6位 (如登录密码)时 关闭输入法

            if (str.length() == maxLength) {
                ViewUtil.hideOneInputMethod(LoginMainActivity.this, mView);
            }
        }
    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".LoginMainActivity">


    <RadioGroup
        android:id="@+id/rg_login"
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">

        <!--        单选框    -->
        <RadioButton
            android:id="@+id/rb_password"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="true"
            android:text="@string/login_by_password"
            android:textColor="@color/black"
            android:textSize="@dimen/common_font_size" />
        <!--        单选框    -->
        <RadioButton
            android:id="@+id/rb_verifycode"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/login_by_vertifycode"
            android:textColor="@color/black"
            android:textSize="@dimen/common_font_size" />


    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:layout_margin="5dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/phone_number"
            android:textColor="@color/black"
            android:textSize="@dimen/common_font_size" />

        <EditText
            android:id="@+id/et_phone"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/edit_select"
            android:hint="@string/input_phone_number"
            android:inputType="number"
            android:maxLength="11"
            android:paddingLeft="10dp"
            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:layout_margin="5dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/login_password"
            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_password"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:background="@drawable/edit_select"
                android:hint="@string/input_password"
                android:inputType="numberPassword"
                android:maxLength="6"
                android:paddingLeft="10dp"
                android:textColor="@color/black"
                android:textColorHint="@color/grey"
                android:textSize="@dimen/common_font_size" />

            <Button
                android:id="@+id/btn_forget"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignEnd="@+id/et_password"
                android:text="@string/forget_password"
                android:textColor="@color/black"
                android:textSize="@dimen/common_font_size" />
        </RelativeLayout>
    </LinearLayout>

    <CheckBox
        android:id="@+id/ck_remember"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginEnd="20dp"
        android:text="@string/remember_password"
        android:textColor="@color/black"
        android:textSize="@dimen/common_font_size" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:textColor="@color/black"
        android:textSize="@dimen/button_font_size" />

</LinearLayout>

LoginForgetActivity.java

package com.tiger.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 verifyCode;
    private EditText et_password_first;
    private EditText et_password_secend;
    private EditText et_verifyCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_forget);

        Intent intent = getIntent();

        mPhone = intent.getStringExtra("phone");
        et_password_first = findViewById(R.id.et_password_first);
        et_password_secend = findViewById(R.id.et_password_secend);
        et_verifyCode = findViewById(R.id.et_verifyCode);
        findViewById(R.id.btn_verifyCode).setOnClickListener(this);
        findViewById(R.id.btn_confirm).setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.btn_verifyCode){
            //生成6位随机的验证码
            verifyCode = String.format("%06d",new Random().nextInt(999999));
            //以下弹出提醒对话框,提示用户记住六位验证码数字
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("请记住验证码");
            builder.setMessage("手机号"+mPhone+",本次验证码是"+verifyCode+",请输入验证码");
            builder.setPositiveButton("好的",null);
            AlertDialog alertDialog = builder.create();
            alertDialog.show();

        }else if (v.getId() == R.id.btn_confirm){
            //点击确定按钮
            String first = et_password_first.getText().toString();
            String second = et_password_secend.getText().toString();
            if (first.length()<6) {
                Toast.makeText(this,"请输入正确的密码",Toast.LENGTH_LONG).show();
                return;
            }

            if (!first.equals(second)){
                Toast.makeText(this,"两次密码不相同",Toast.LENGTH_LONG).show();
                return;
            }

            if (!et_verifyCode.getText().toString().equals(verifyCode)){
                Toast.makeText(this,"输入的验证码不正确",Toast.LENGTH_LONG).show();
                return;
            }
            Toast.makeText(this,"密码修改成功",Toast.LENGTH_LONG).show();
            //把修改好的密码返回给上一个页面
            Intent intent = new Intent();
            intent.putExtra("new_password",first);

            setResult(Activity.RESULT_OK,intent);

            finish();
        }


    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".LoginMainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal"
        android:layout_margin="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/inout_new_password"
            android:textColor="@color/black"
            android:textSize="@dimen/common_font_size"
            android:gravity="center"/>
        <EditText
            android:id="@+id/et_password_first"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@drawable/edit_select"
            android:hint="@string/inout_new_password_hint"
            android:paddingLeft="10dp"
            android:inputType="number"
            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"
        android:layout_margin="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/confirm_new_password"
            android:textColor="@color/black"
            android:textSize="@dimen/common_font_size"
            android:gravity="center"/>
        <EditText
            android:id="@+id/et_password_secend"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@drawable/edit_select"
            android:hint="@string/confirm_new_password_again"
            android:paddingLeft="10dp"
            android:inputType="number"
            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:layout_margin="5dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/verifycode"
            android:textColor="@color/black"
            android:textSize="@dimen/common_font_size"
            android:gravity="center"/>

        <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_marginStart="23dp"
                android:layout_marginTop="5dp"
                android:layout_marginEnd="5dp"
                android:layout_marginBottom="5dp"
                android:background="@drawable/edit_select"
                android:hint="@string/input_verifycode"
                android:inputType="numberPassword"
                android:maxLength="6"
                android:paddingLeft="10dp"
                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:text="@string/get_verifycode"
                android:layout_alignEnd="@+id/et_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>

viewUtil 

package com.tiger.chapter05.util;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class ViewUtil {
    public static  void hideOneInputMethod(Activity act, View view){
        //从系统服务中获取输入法管理器
        InputMethodManager manager = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
        //关闭屏幕上的输入法软键盘
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值