Android倒数计时器解释通缉

时间:2014-09-09 18:00:29

标签: android countdowntimer

在android参考资料中我发现了一个关于倒计时器的页面:CountDownTimers

它里面有这段代码:

    new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

我想知道它是什么以及它是一个对象或类,以及我如何将它用作不同类对象的计时器。有人可以解释我将如何把它放入我的android项目并使用它。三江源。

3 个答案:

答案 0 :(得分:7)

Public Constructors:

CountDownTimer(long millisInFuture, long countDownInterval)

公共方法:

final void  cancel()
    Cancel the countdown.

abstract void   onFinish()
    Callback fired when the time is up.

abstract void   onTick(long millisUntilFinished)
    Callback fired on regular interval.

synchronized final CountDownTimer   start()
    Start the countdown.

Public Constructors:

public CountDownTimer (long millisInFuture, long countDownInterval)
Parameters
1. millisInFuture   The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
2. countDownInterval    The interval along the way to receive onTick(long) callbacks.

公共方法:

public final void cancel ()
    Cancel the countdown.

public abstract void onFinish ()
    Callback fired when the time is up.

public abstract void onTick (long millisUntilFinished)
    Callback fired on regular interval.
        Parameters: millisUntilFinished     The amount of time until finished.

public final synchronized CountDownTimer start ()
    Start the countdown.

教程链接:

link 1

link 2

link 3

修改

new CountDownTimer(30000 /*For how long should timer run*/, 1000 /*time interval after which `onTick()` should be called*/) {

 public void onTick(long millisUntilFinished) {
     Log.i("Countdown Timer: ","seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     //Done timer time out.
     myTextView.setText("Time Out.!");
 }
}.start();

答案 1 :(得分:0)

您引用的参考文献非常清楚地说明了这一点。

CountDownTimer是一个 CLASS ,它用于执行已确定的任务,直到完成为止。

onTick 方法用于对每个刻度(以毫秒为单位)执行操作,

public void onTick(long millisUntilFinished) {
     //It will perform this task every millisecond until countdown finishes.
 }

onFinish 方法执行最后一项任务,直到课程结束其实例。

 public void onFinish() {
     //It will perform this task only once, when the countdown finishes.
 } 

答案 2 :(得分:0)

From the docs

  

公共抽象类

     

CountDownTimer

     

扩展对象

应回答一个问题。

至于如何使用它,see this answer