Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能
在 Android 中倒計(jì)時(shí)功能是比較常用的一個(gè)功能,比如短信驗(yàn)證碼,付款倒計(jì)時(shí)等。實(shí)現(xiàn)方式有Handler、Thread 等,但是實(shí)現(xiàn)起來都有點(diǎn)麻煩,其實(shí)Android已經(jīng)為我們封裝好了一個(gè)抽象類 CountDownTimer,可以簡(jiǎn)單的實(shí)現(xiàn)倒計(jì)時(shí)功能,如下圖所示。
CountDownTimer 實(shí)現(xiàn)倒計(jì)時(shí)功能的機(jī)制也是用Handler 消息控制,只是它幫我們已經(jīng)封裝好了,先看一下它的介紹。
Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field: new CountDownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { mTextField.setText("seconds remaining: " + millisUntilFinished / 1000); } public void onFinish() { mTextField.setText(“done!”); } }.start();
大致意思是,設(shè)置一個(gè)倒計(jì)時(shí),直到完成這個(gè)時(shí)間段的計(jì)時(shí),并會(huì)實(shí)時(shí)更新時(shí)間的變化,最后舉了一個(gè)30秒倒計(jì)時(shí)的例子,如下:
new CountDownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { mTextField.setText("seconds remaining: " + millisUntilFinished / 1000); } public void onFinish() { mTextField.setText("done!"); } }.start();
詳解
可以看到,上面示例中構(gòu)造方法需要傳入兩個(gè)參數(shù),如下:
/** * @param millisInFuture The number of millis in the future from the call to start() * until the countdown is done and onFinish() is called. * @param countDownInterval The interval along the way to receive onTick(long) callbacks. */ public CountDownTimer(long millisInFuture, long countDownInterval) { mMillisInFuture = millisInFuture; mCountdownInterval = countDownInterval; }
第一個(gè)參數(shù)是倒計(jì)時(shí)的總時(shí)間,第二個(gè)參數(shù)是倒計(jì)時(shí)的時(shí)間間隔(每隔多久執(zhí)行一次),注意這里傳入的兩個(gè)時(shí)間參數(shù)的單位都是毫秒。
它提供的幾個(gè)方法也很簡(jiǎn)單,如下:
- start():開始倒計(jì)時(shí)。
- cancel():取消倒計(jì)時(shí)。
- onFinish():倒計(jì)時(shí)完成后回調(diào)。
- onTick(long millisUnitilFinished):當(dāng)前任務(wù)每完成一次倒計(jì)時(shí)間隔時(shí)間時(shí)回調(diào)。
驗(yàn)證碼示例
短信驗(yàn)證碼倒計(jì)時(shí)原理很簡(jiǎn)單,也就是點(diǎn)擊獲取驗(yàn)證碼開啟倒計(jì)時(shí),在倒計(jì)時(shí)內(nèi)不可點(diǎn)擊,倒計(jì)時(shí)結(jié)束后方可重新獲取,如下所示:
new CountDownTimer(millisUntilFinished, 1000) { /** * 當(dāng)前任務(wù)每完成一次倒計(jì)時(shí)間隔時(shí)間時(shí)回調(diào) * @param millisUntilFinished */ public void onTick(long millisUntilFinished) { if (btn_Code != null) { //按鈕不可用 btn_Code.setClickable(false); btn_Code.setEnabled(false); btn_Code.setText(millisUntilFinished / 1000 + "s"); } } /** * 倒計(jì)時(shí)完成后回調(diào) */ public void onFinish() { if (btn_Code != null) { //按鈕可用 btn_Code.setText("重新獲取"); btn_Code.setClickable(true); btn_Code.setEnabled(true); } //取消倒計(jì)時(shí) cancel(); } }.start();
注:在Activity或Fragment銷毀的時(shí)候記得調(diào)用 cancle() 方法,否則它的 onTick() 方法還會(huì)繼續(xù)執(zhí)行,容易造成內(nèi)存泄漏。
總結(jié)
以上所述是小編給大家介紹的Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
上一篇:Android 自定義球型水波紋帶圓弧進(jìn)度效果(實(shí)例代碼)
欄 目:Android
下一篇:Android自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕
本文標(biāo)題:Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能
本文地址:http://mengdiqiu.com.cn/a1/Android/9020.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?