Android實(shí)現(xiàn)倒計(jì)時(shí)CountDownTimer使用詳解
在開發(fā)中會(huì)經(jīng)常用到倒計(jì)時(shí)這個(gè)功能,包括給手機(jī)發(fā)送驗(yàn)證碼等等,之前我的做法都是使用Handler + Timer + TimerTask來實(shí)現(xiàn),現(xiàn)在發(fā)現(xiàn)了這個(gè)類,果斷拋棄之前的做法,相信還是有很多人和我一樣一開始不知道Android已經(jīng)幫我們封裝好了一個(gè)叫CountDownTimer的類。
從字面上就可以看出來它叫倒數(shù)計(jì)時(shí)器又稱定時(shí)器或計(jì)時(shí)器,采用Handler的方式實(shí)現(xiàn),將后臺(tái)線程的創(chuàng)建和Handler隊(duì)列封裝而成。
看了一下源碼,發(fā)現(xiàn)這個(gè)類的調(diào)用還蠻簡單,只有四個(gè)方法:
(1)public abstract void onTick(long millisUntilFinished);
固定間隔被調(diào)用
(2)public abstract void onFinish();
倒計(jì)時(shí)完成時(shí)被調(diào)用
(3)public synchronized final void cancel():
取消倒計(jì)時(shí),當(dāng)再次啟動(dòng)會(huì)重新開始倒計(jì)時(shí)
(4)public synchronized final CountDownTimer start():
啟動(dòng)倒計(jì)時(shí)
在這里可以看到前面兩個(gè)是抽象方法,需要重寫。
簡單看一下代碼:
package com.per.countdowntimer; import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTvShow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTvShow = (TextView) findViewById(R.id.show); } /** * 取消倒計(jì)時(shí) * @param v */ public void oncancel(View v) { timer.cancel(); } /** * 開始倒計(jì)時(shí) * @param v */ public void restart(View v) { timer.start(); } private CountDownTimer timer = new CountDownTimer(10000, 1000) { @Override public void onTick(long millisUntilFinished) { mTvShow.setText((millisUntilFinished / 1000) + "秒后可重發(fā)"); } @Override public void onFinish() { mTvShow.setEnabled(true); mTvShow.setText("獲取驗(yàn)證碼"); } }; }
順帶附上XML布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:onClick="restart" android:text="取消" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:onClick="oncancel" android:text="結(jié)束" /> </LinearLayout>
最后說明一下:
CountDownTimer timer = new CountDownTimer(10000, 1000):以毫秒為單位,第一個(gè)參數(shù)是指從開始調(diào)用start()方法到倒計(jì)時(shí)完成的時(shí)候onFinish()方法被調(diào)用這段時(shí)間的毫秒數(shù),也就是倒計(jì)時(shí)總的時(shí)間;第二個(gè)參數(shù)表示間隔多少毫秒調(diào)用一次 onTick方法,例如間隔1000毫秒。
在調(diào)用的時(shí)候直接使用timer.start();
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
外層豎向ScrollView,里層橫向ScrollView滑動(dòng)沖突的解決方法
下面小編就為大家?guī)硪黄鈱迂Q向ScrollView,里層橫向ScrollView滑動(dòng)沖突的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04Android使用CountDownTimer模擬短信驗(yàn)證倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android使用CountDownTimer模擬短信驗(yàn)證倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android自定義實(shí)現(xiàn)轉(zhuǎn)盤菜單
旋轉(zhuǎn)菜單是一種占用空間較大,實(shí)用性稍弱的UI,本文主要為大家詳細(xì)介紹了Android如何自定義實(shí)現(xiàn)轉(zhuǎn)盤菜單,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2023-12-12Android開發(fā)中的MVC設(shè)計(jì)模式淺析
這篇文章主要介紹了Android開發(fā)中的MVC設(shè)計(jì)模式淺析,本文講解了對(duì)Android開發(fā)中的MVC設(shè)計(jì)模式的理解,需要的朋友可以參考下2015-06-06Android編程之短信竊聽器實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程之短信竊聽器實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了Android編程實(shí)現(xiàn)竊聽器的具體步驟與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android避免內(nèi)存溢出(Out of Memory)方法匯總
這篇文章主要為大家詳細(xì)介紹了Android避免內(nèi)存溢出Out of Memory方法匯總,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android Retrofit文件下載進(jìn)度顯示問題的解決方法
這篇文章主要為大家詳細(xì)介紹了Android Retrofit文件下載進(jìn)度顯示問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android編程入門之HelloWorld項(xiàng)目目錄結(jié)構(gòu)分析
這篇文章主要介紹了Android編程入門之HelloWorld項(xiàng)目目錄結(jié)構(gòu)分析,較為詳細(xì)的分析了Android項(xiàng)目的目錄結(jié)構(gòu)與具體作用,需要的朋友可以參考下2015-12-12