Android使用CountDownTimer實現倒計時效果_Android

來源:互聯網
上載者:User

在開發中會經常用到倒計時這個功能,包括給手機發送驗證碼等等,之前我的做法都是使用Handler + Timer + TimerTask來實現,現在發現了這個類,果斷拋棄之前的做法,相信還是有很多人和我一樣一開始不知道Android已經幫我們封裝好了一個叫CountDownTimer的類。

從字面上就可以看出來它叫倒數計時器又稱定時器或計時器,採用Handler的方式實現,將後台線程的建立和Handler隊列封裝而成。
看了一下源碼,發現這個類的調用還蠻簡單,只有四個方法:

(1)public abstract void onTick(long millisUntilFinished);
固定間隔被調用

(2)public abstract void onFinish();
倒計時完成時被調用

(3)public synchronized final void cancel():
取消倒計時,當再次啟動會重新開始倒計時

(4)public synchronized final CountDownTimer start():
啟動倒計時

在這裡可以看到前面兩個是抽象方法,需要重寫。

簡單看一下代碼:

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);  }  /**   * 取消倒計時   * @param v   */  public void oncancel(View v) {    timer.cancel();  }  /**   * 開始倒計時   * @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) + "秒後可重發");    }    @Override    public void onFinish() {      mTvShow.setEnabled(true);      mTvShow.setText("擷取驗證碼");    }  };}

順帶附上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="結束" /></LinearLayout>

最後說明一下:
CountDownTimer timer = new CountDownTimer(10000, 1000):以毫秒為單位,第一個參數是指從開始調用start()方法到倒計時完成的時候onFinish()方法被調用這段時間的毫秒數,也就是倒計時總的時間;第二個參數表示間隔多少毫秒調用一次 onTick方法,例如間隔1000毫秒。
在調用的時候直接使用timer.start();

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.