Android中CountDownTimer 實(shí)現(xiàn)倒計(jì)時(shí)功能
CountDownTimer
CountDownTimer 是android 自帶的一個(gè)倒計(jì)時(shí)類,使用這個(gè)類可以很簡(jiǎn)單的實(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è)判斷即可。
以上就是本文的全部?jī)?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資源文件與層次式導(dǎo)航超詳細(xì)講解
這篇文章主要介紹了Android資源文件與層次式導(dǎo)航,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
Android使用Handler實(shí)現(xiàn)定時(shí)器與倒計(jì)時(shí)器功能
Handler的最常見應(yīng)用場(chǎng)景之一便是通過Handler在子線程中間接更新UI。這篇文章主要介紹了Android使用Handler實(shí)現(xiàn)定時(shí)器與倒計(jì)時(shí)器功能,需要的朋友可以參考下2018-02-02
Android編程實(shí)現(xiàn)仿優(yōu)酷圓盤旋轉(zhuǎn)菜單效果的方法詳解【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿優(yōu)酷圓盤旋轉(zhuǎn)菜單效果的方法,涉及Android界面布局及事件響應(yīng)相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-08-08
Android主項(xiàng)目與Module中R類的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Android主項(xiàng)目與Module中R類的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02
Android應(yīng)用中實(shí)現(xiàn)選擇本地文件與目錄的實(shí)例分享
這篇文章主要介紹了Android應(yīng)用中實(shí)現(xiàn)選擇本地文件與目錄的實(shí)例分享,相當(dāng)于從app內(nèi)呼出的簡(jiǎn)易的資源管理器來使用本地資源,需要的朋友可以參考下2016-02-02
詳解Android Studio實(shí)現(xiàn)用戶登陸界面demo(xml實(shí)現(xiàn))
這篇文章主要介紹了詳解Android Studio實(shí)現(xiàn)用戶登陸界面demo,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android IPC進(jìn)程間通信詳解最新AndroidStudio的AIDL操作)
這篇文章主要介紹了Android IPC進(jìn)程間通信的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android 中TextView的使用imageview被壓縮問題解決辦法
這篇文章主要介紹了Android 中TextView的使用imageview被壓縮問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04

