網(wǎng)站首頁(yè) 編程語(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
相關(guān)推薦
- 2022-08-23 Shell?腳本自動(dòng)輸入密碼的三種方式小結(jié)_linux shell
- 2022-06-22 使用Git向GitHub上傳更新內(nèi)容_其它綜合
- 2022-04-28 WPF使用WrapPanel環(huán)繞面板布局_實(shí)用技巧
- 2021-12-09 Linux環(huán)境下安裝JDK1.8_Linux
- 2022-11-26 pytorch邏輯回歸實(shí)現(xiàn)步驟詳解_python
- 2022-07-20 虛擬機(jī)下的/dev/sda1占用率為100%
- 2022-12-13 C++?Boost?StringAlgorithms超詳細(xì)講解_C 語(yǔ)言
- 2022-12-06 靜態(tài)pod?創(chuàng)建使用示例詳解_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支