Android 发送手机号验证码倒计时

.xml文件
在这里插入图片描述
获取验证码倒计时通过TimeCount 计时器实现的,记得在倒计时过程中将按钮设置为禁用

<LinearLayout
        android:layout_marginTop="@dimen/dp_100"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp_20"
        android:paddingTop="@dimen/dp_50"
        android:layout_marginRight="@dimen/dp_20"
        android:paddingBottom="@dimen/dp_50"
        android:background="@color/white"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_35"
            android:layout_marginLeft="@dimen/dp_5"
            android:layout_marginRight="@dimen/dp_5"
            android:background="@drawable/bk_register"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="@dimen/dp_24"
                android:layout_height="@dimen/dp_24"
                android:layout_gravity="center"
                android:layout_marginLeft="@dimen/dp_10"
                android:src="@drawable/ic_phone_iphone" />

            <EditText
                android:id="@+id/et_phone"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/dp_6"
                android:background="@null"
                android:textSize="@dimen/sp_16"
                android:hint="请输入手机号码" />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_35"
            android:layout_marginLeft="@dimen/dp_5"
            android:layout_marginTop="@dimen/dp_10"
            android:layout_marginRight="@dimen/dp_5"
            android:background="@drawable/bk_register">

            <ImageView
                android:layout_width="@dimen/dp_24"
                android:layout_height="@dimen/dp_24"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/dp_10"
                android:src="@drawable/ic_lock_black" />

            <EditText
                android:textSize="@dimen/sp_16"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/dp_40"
                android:background="@null"
                android:hint="请输入验证码" />

            <TextView
                android:id="@+id/tv_yzm"
                android:textSize="@dimen/sp_16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/dp_10"
                android:textColor="#0a71b0"
                android:text="获取验证码" />
        </RelativeLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_35"
            android:layout_marginLeft="@dimen/dp_5"
            android:layout_marginTop="@dimen/dp_10"
            android:layout_marginRight="@dimen/dp_5"
            android:background="@drawable/bg_setting_button"
            android:text="确认"
            android:textColor="@color/white"
            android:textSize="@dimen/sp_14" />
    </LinearLayout>

.java文件

  private TextView tvYzm;
  private EditText etPhone;
  private TimeCount time;
  ...
  onCrate(Bundle savedInstanceState){
  ...
      time = new TimeCount(60000, 1000);

        tvYzm = findViewById(R.id.tv_yzm);
        etPhone = findViewById(R.id.et_phone);
        tvYzm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    etPhone.setError("请先输入手机号");
                    return;
                }
                if (!isMobileNO(etPhone.getText().toString())) {
                    etPhone.setError("手机号格式不正确");
                    return;
                }
                time.start();//成功按钮发送验证码倒计时
            }
        });}

工具

 //验证码倒计时
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisinfuture, long countdowninterval) {
            super(millisinfuture, countdowninterval);
        }

        @Override
        public void onTick(long l) {
            tvYzm.setClickable(false);
            tvYzm.setText("(" + l / 1000 + ")s");
            tvYzm.setTextColor(getResources().getColor(R.color.gray));
        }

        @Override
        public void onFinish() {
            tvYzm.setText("发送验证码");
            tvYzm.setClickable(true);
        }
    }

    //判断手机号码格式是否正确
    private boolean isMobileNO(String mobiles) {
        String telRegex = "^((1[3,5,7,8][0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        if (TextUtils.isEmpty(mobiles)) {
            return false;
        } else {
            return mobiles.matches(telRegex);
        }
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值