欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)倒計(jì)時(shí)CountDownTimer使用詳解

 更新時(shí)間:2017年12月27日 16:20:19   作者:Adan0520  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)倒計(jì)時(shí)CountDownTimer的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在開發(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)文章

最新評(píng)論