網(wǎng)站首頁 編程語言 正文
一、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();
? ? ? ? /**
? ? ? ? ?* 調(diào)用獲取驗證碼接口
? ? ? ? ?* 測試手機號:1234567891
? ? ? ? ?* */
? ? ? ?// getYZMRequest(userPhone);
? ? } else {
? ? ? ? showToast("請輸入正確手機號");
? ? }
}
TextView btn_yanzhengma : 發(fā)送驗證碼的的按鈕,EditText edit_Phone ?: 輸入手機號的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 = "獲取驗證碼"
? ? ? ? }
? ? ? ? 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
?/**
????* 獲取驗證碼 loginSendSmsCode按鈕的點擊事件
??* */
????????loginSendSmsCode.setOnClickListener {
????????????if (timer == null) {
????????????????timer = TimerUnit(loginSendSmsCode)
????????????}
????????????timer?.startTime()
????????}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-END
原文鏈接:https://lizhong.blog.csdn.net/article/details/122835072
相關(guān)推薦
- 2022-08-17 Qt?QFrame的具體使用_C 語言
- 2022-09-21 Flutter自定義底部導(dǎo)航欄的方法_Android
- 2022-08-17 Python?pandas.replace的用法詳解_python
- 2022-07-12 mac端口號被占用解決辦法
- 2022-10-02 SpringBoot前端后端分離之Nginx服務(wù)器下載安裝過程_nginx
- 2022-06-21 Android?studio實現(xiàn)兩個界面間的切換_Android
- 2023-04-06 python?numpy.linalg.norm函數(shù)的使用及說明_python
- 2022-05-31 ASP.NET?Core使用NLog記錄日志_實用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支