Android中CountDownTimer 實(shí)現(xiàn)倒計(jì)時(shí)功能
CountDownTimer
CountDownTimer 是android 自帶的一個(gè)倒計(jì)時(shí)類,使用這個(gè)類可以很簡單的實(shí)現(xiàn) 倒計(jì)時(shí)功能
CountDownTimer 的實(shí)現(xiàn)方式
new CountDownTimer(6000,1000) {//第一個(gè)參數(shù)表示的是倒計(jì)時(shí)的總時(shí)間,第二參數(shù)表示的是倒計(jì)時(shí)的間隔時(shí)間。 @Override public void onTick(long millisUntilFinished) {//倒計(jì)時(shí)的過程 textView.setText(millisUntilFinished / 1000 + "秒"); } @Override public void onFinish() {//倒計(jì)時(shí)結(jié)束 textView.setText("倒計(jì)時(shí)結(jié)束"); } }.start();
實(shí)現(xiàn)效果
取消計(jì)時(shí)器
調(diào)用 CountDownTimer 的 cancel() 方法,可以為我們?nèi)∠?jì)時(shí)器:但是這個(gè)方法,只有在 android 5.0 以上才有效果,在android 5.0 以下并沒有效果。如果需要在android 5.0 以下的系統(tǒng)中也使用 cancel,需要我們自己根據(jù) CountDownTimer 源碼中的 實(shí)現(xiàn)方式,重新實(shí)現(xiàn)一下。
/** * Cancel the countdown. */ public synchronized final void cancel() { mCancelled = true; mHandler.removeMessages(MSG); } private static final int MSG = 1; // handles counting down private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { synchronized (CountDownTimer.this) { if (mCancelled) { return; } final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime(); if (millisLeft <= 0) { onFinish(); } else if (millisLeft < mCountdownInterval) { // no tick, just delay until done sendMessageDelayed(obtainMessage(MSG), millisLeft); } else { long lastTickStart = SystemClock.elapsedRealtime(); onTick(millisLeft); // take into account user's onTick taking time to execute long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime(); // special case: user's onTick took more than interval to // complete, skip to next interval while (delay < 0) delay += mCountdownInterval; sendMessageDelayed(obtainMessage(MSG), delay); } } } };
由于在 android 5.0以上 增加了一個(gè)
private boolean mCancelled = false;
所以我們只需要在 5.0 以下的系統(tǒng)中,去掉
if (mCancelled) { return; }
去掉這個(gè)判斷即可。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用CountDownTimer模擬短信驗(yàn)證倒計(jì)時(shí)
- Android CountDownTimer實(shí)現(xiàn)定時(shí)器和倒計(jì)時(shí)效果
- Android使用CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘
- Android實(shí)現(xiàn)倒計(jì)時(shí)CountDownTimer使用詳解
- Android利用CountDownTimer實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)效果實(shí)例
- Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer)
- Android利用CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能 Android實(shí)現(xiàn)停留5s跳轉(zhuǎn)到登錄頁面
- Android CountDownTimer案例總結(jié)
相關(guān)文章
Android實(shí)現(xiàn)圖片預(yù)覽與保存功能
在App開發(fā)中,通常為了省流提高加載速度提升用戶體驗(yàn)我們通常在列表中或新聞中的插圖都是以縮略圖壓縮過的圖片來進(jìn)行展示,當(dāng)用戶點(diǎn)擊圖片時(shí)我們?cè)偃ゼ虞d真正像素的大圖讓用戶預(yù)覽。本文將利用Flutter實(shí)現(xiàn)這一功能,需要的可以參考一下2022-04-04Android 軟鍵盤狀態(tài)并隱藏輸入法的實(shí)例
這篇文章主要介紹了Android 軟鍵盤狀態(tài)并隱藏輸入法的實(shí)例的相關(guān)資料,這里提供實(shí)例實(shí)現(xiàn)軟鍵盤切換并隱藏輸入法的鍵盤,需要的朋友可以參考下2017-09-09Android中轉(zhuǎn)場動(dòng)畫的實(shí)現(xiàn)與兼容性處理
大家都知道Android 中的動(dòng)畫有很多,除了在一個(gè)界面上使用幀動(dòng)畫、屬性動(dòng)畫將一個(gè)或多個(gè) View 進(jìn)行動(dòng)畫處理以外,還可以用于兩個(gè)界面之間過渡、跳轉(zhuǎn)。本文的內(nèi)容包括:Android 5.0+ 的轉(zhuǎn)場動(dòng)畫和Android 4.X 模擬實(shí)現(xiàn) Android 5.0+ 轉(zhuǎn)場效果。有需要的可以參考借鑒。2016-10-10Android實(shí)現(xiàn)萬能自定義陰影控件實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)萬能自定義陰影控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Android之帶group指示器的ExpandableListView(自寫)
Android缺省的ExpandableListView的group header無法固定在界面上,在網(wǎng)上搜索了好多都不盡人意,于是乎在別人的基礎(chǔ)上改進(jìn)了一點(diǎn)點(diǎn),原理都差不多2013-06-06