欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android与Kotlin发送验证码倒计时?

程序员文章站 2022-06-21 20:02:45
一、Android1.自定义倒计时方法/** * 获取验证码 */public void getYanZhengMa(TextView btn_yanzhengma, EditText edit_Phone) { userPhone = edit_Phone.getText().toString().trim(); if (!TextUtils.isEmpty(userPhone) && userPhone.length() == 11) {...

一、Android

1.自定义倒计时方法

/**
 * 获取验证码
 */
public void getYanZhengMa(TextView btn_yanzhengma, EditText edit_Phone) {
    userPhone = edit_Phone.getText().toString().trim();
    if (!TextUtils.isEmpty(userPhone) && userPhone.length() == 11) {
        /** 倒计时60秒,一次1秒 */
        CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                btn_yanzhengma.setTextColor(getResources().getColor(R.color.gray_light));
                btn_yanzhengma.setText("还剩" + millisUntilFinished / 1000 + "秒");
                btn_yanzhengma.setEnabled(false);
            }

            @Override
            public void onFinish() {
                btn_yanzhengma.setText("请重新获取");
                btn_yanzhengma.setTextColor(getResources().getColor(R.color.yanzhengma_color));
                btn_yanzhengma.setEnabled(true);
            }
        }.start();
        /**
         * 调用获取验证码接口
         * 测试手机号:18089212658
         * */
       // getYZMRequest(userPhone);
    } else {
        showToast("请输入正确手机号");
    }
}

说明:TextView btn_yanzhengma : 发送验证码的的按钮

          EditText edit_Phone  : 输入手机号的EditText

二、Kotlin

1.创建倒计时类

package com.june.lib.utils

import android.os.Handler
import android.widget.TextView

/**
 *@author:FengJun.wu
 *@date:2020-03-20 23:23
 *@discription:
**/

class TimerUnit(private val textView: TextView) : Handler() {
    private var defaultTime = 60
    private var time = defaultTime
    private var isShowEndText = true

    private var timeEndListener: OnTimeEndListener? = null

    private var runnable: Runnable = object : Runnable {
        override fun run() {
            time--
            if (time == 0) {
                endtTime()
                return
            }
            textView.text = String.format("%ds", time)
            postDelayed(this, 1000)
        }
    }

    fun setTimeEndListener(timeEndListener: OnTimeEndListener) {
        this.timeEndListener = timeEndListener
    }

    fun setShowEndText(showEndText: Boolean) {
        isShowEndText = showEndText
    }


    fun setTime(time: Int) {
        this.defaultTime = time
        this.time = defaultTime
    }

    fun startTime() {
        post(runnable)
        textView.isEnabled = false
    }


    fun pauseTime() {
        removeCallbacks(runnable)
        time = defaultTime
    }

    fun endtTime() {
        if (isShowEndText) {
            textView.text = "重新获取"
        }
        textView.isEnabled = true
        removeCallbacks(runnable)
        time = defaultTime
        if (timeEndListener != null) {
            timeEndListener!!.timeEnd()
        }
    }

    interface OnTimeEndListener {
        fun timeEnd()
    }

}

2. 在Activity中调用

private var timer: TimerUnit? = null
 /**
    * 获取验证码 按钮的点击事件
  * */
        loginSendSmsCode.setOnClickListener {
            if (timer == null) {
                timer = TimerUnit(loginSendSmsCode)
            }
            timer?.startTime()
        }

总结:都能实现自定义倒计时的时间,并且倒计时开始后,不到结束时间按钮是不能点击的。

 

 

本文地址:https://blog.csdn.net/yyxhzdm/article/details/108589734