?
Ce document utilise Manuel du site Web PHP chinois Libérer
CountDownTimer
版本:Android 4.0 r1
?
結(jié)構(gòu)
繼承關(guān)系
public abstract class CountDownTimer extends Object
????????
java.lang.Object
android.os.CountDownTimer
?
類概述
定時(shí)執(zhí)行在一段時(shí)候后停止的倒計(jì)時(shí),在倒計(jì)時(shí)執(zhí)行過程中會(huì)在固定間隔時(shí)間得到通知(譯者:觸發(fā)onTick方法),下面的例子顯示在一個(gè)文本框中顯示一個(gè)30s倒計(jì)時(shí):
?new CountdownTimer(30000, 1000) {
? ? ?public void onTick(long millisUntilFinished) {
? ? ? ? ?mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
? ? ?}
? ? ?public void onFinish() {
? ? ? ? ?mTextField.setText("done!");
? ? ?}
? }.start();?
onTick的調(diào)用是同步的,保證這次調(diào)用不會(huì)在之前調(diào)用完成前發(fā)生。這里的同步機(jī)制主要是用來:onTick的實(shí)現(xiàn)需要很多時(shí)間執(zhí)行比倒計(jì)時(shí)間隔更重要的事情。
?
構(gòu)造函數(shù)
???????? public CountDownTimer (long millisInFuture, long countDownInterval)
參數(shù)
?????????????????? millisInFuture? 從開始調(diào)用start()到倒計(jì)時(shí)完成并onFinish()方法被調(diào)用的毫秒數(shù)。(譯者注:倒計(jì)時(shí)時(shí)間,單位毫秒)
?????????????????? countDownInterval ? 接收onTick(long)回調(diào)的間隔時(shí)間。(譯者注:?jiǎn)挝缓撩耄?/span>
?
公共方法
public final void cancel ()
???????? 取消倒計(jì)時(shí)(譯者:取消后,再次啟動(dòng)會(huì)重新開始倒計(jì)時(shí))????????
??????????????????
public abstract void onFinish ()
???????? 倒計(jì)時(shí)完成時(shí)被調(diào)用????
?
public abstract void onTick (long millisUntilFinished)
???????? 固定間隔被調(diào)用
參數(shù)
??????????????????????????? millisUntilFinished?? 倒計(jì)時(shí)剩余時(shí)間。
?
public synchronized final CountDownTimer start ()
???????? 啟動(dòng)倒計(jì)時(shí)
?
補(bǔ)充
文章精選
Android 定時(shí)器
android倒計(jì)時(shí)功能的實(shí)現(xiàn)(CountDownTimer)
示例代碼
???????? Java:
package com.test.countdowntimer; ? import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; ? import com.test.R; ? public class CountDownTimeActivity extends Activity implements OnClickListener { ? ??? TextView mTextView; ??? Button mButton1; ??? Button mButton2; ??? ??? @Override ??? protected void onCreate(Bundle savedInstanceState) { ?????? super.onCreate(savedInstanceState); ?????? ?????? setContentView(R.layout.countdown); ?????? ??? ??? mTextView = (TextView)findViewById(R.id.textView1); ?????? mButton1 = (Button)findViewById(R.id.button1); ?????? mButton2 = (Button)findViewById(R.id.button2); ?????? mButton1.setOnClickListener(this); ?????? mButton2.setOnClickListener(this); ??? } ? ??? CountDownTimer timer = new CountDownTimer(40000,1000) { ?????? ?????? @Override ?????? public void onTick(long millisUntilFinished) { ?????????? mTextView.setText("seconds remaining: " + millisUntilFinished / 1000); ?????????? try { ????????????? Thread.sleep(1200); ?????????? } catch (InterruptedException e) { ????????????? e.printStackTrace(); ?????????? } ?????????? Log.e("CountDown",millisUntilFinished+""); ?????? } ?????? ?????? @Override ?????? public void onFinish() { ?????????? mTextView.setText("done!"); ?????? } ??? }; ??? ??? @Override ??? public void onClick(View v) { ?????? switch(v.getId()){ ?????? case R.id.button1: ?????????? timer.start(); ?????????? break; ?????? case R.id.button2: ?????????? timer.cancel(); ?????????? break; ?????? } ?????? ??? } } ? |
?
???????? XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout ? xmlns:android="http://schemas.android.com/apk/res/android" ? android:orientation="vertical" ? android:layout_width="match_parent" ? android:layout_height="match_parent"> ??? <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> ??? <Button android:text="開始" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> ??? <Button android:text="取消" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> ??? </LinearLayout> ? |
說明:
CountDownTimer的間隔為1s,我們?cè)?/span>onTIck方法里面休眠了1.2s,所以log出來發(fā)現(xiàn)打印間隔變成了2s,即中間一次onTick方法沒有被執(zhí)行(不會(huì)在之前一次調(diào)用完成前被調(diào)用)。
?