日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

android原生與kotlin驗(yàn)證碼倒計(jì)時(shí)

作者:generallizhong 更新時(shí)間: 2022-04-11 編程語(yǔ)言

一、Android原生倒計(jì)時(shí)代碼

1、倒計(jì)時(shí)方法

/**
?* 獲取驗(yàn)證碼
?*/
public void getYanZhengMa(TextView btn_yanzhengma, EditText edit_Phone) {
? ? userPhone = edit_Phone.getText().toString().trim();
? ? if (!TextUtils.isEmpty(userPhone) && userPhone.length() == 11) {
? ? ? ? /** 倒計(jì)時(shí)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("請(qǐng)重新獲取");
? ? ? ? ? ? ? ? btn_yanzhengma.setTextColor(getResources().getColor(R.color.yanzhengma_color));
? ? ? ? ? ? ? ? btn_yanzhengma.setEnabled(true);
? ? ? ? ? ? }
? ? ? ? }.start();
? ? ? ? /**
? ? ? ? ?* 調(diào)用獲取驗(yàn)證碼接口
? ? ? ? ?* 測(cè)試手機(jī)號(hào):1234567891
? ? ? ? ?* */
? ? ? ?// getYZMRequest(userPhone);
? ? } else {
? ? ? ? showToast("請(qǐng)輸入正確手機(jī)號(hào)");
? ? }
}


TextView btn_yanzhengma : 發(fā)送驗(yàn)證碼的的按鈕,EditText edit_Phone ?: 輸入手機(jī)號(hào)的EditText

二、Kotlin代碼



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

/**
?*@date:2022-02-9 
?*@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 = "獲取驗(yàn)證碼"
? ? ? ? }
? ? ? ? textView.isEnabled = true
? ? ? ? removeCallbacks(runnable)
? ? ? ? time = defaultTime
? ? ? ? if (timeEndListener != null) {
? ? ? ? ? ? timeEndListener!!.timeEnd()
? ? ? ? }
? ? }

? ? interface OnTimeEndListener {
? ? ? ? fun timeEnd()
? ? }

}

2. 在kotlin中使用地方調(diào)用代碼

private var timer: TimerUnit? = null
?/**
????* 獲取驗(yàn)證碼 loginSendSmsCode按鈕的點(diǎn)擊事件
??* */
????????loginSendSmsCode.setOnClickListener {
????????????if (timer == null) {
????????????????timer = TimerUnit(loginSendSmsCode)
????????????}
????????????timer?.startTime()
????????}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-END

原文鏈接:https://lizhong.blog.csdn.net/article/details/122835072

欄目分類
最近更新