2016/03/14

Android 倒數計時器

倒數計時可以分為兩種

1.可重複的  使用 Thread

2.不可重複的 使用 CountDownTimer()

官網的解釋:
http://developer.android.com/reference/android/os/CountDownTimer.html

Public Methods


public final synchronized void cancel ()

Added in API level 1
Cancel the countdown.

public abstract void onFinish ()

Added in API level 1
Callback fired when the time is up.

public abstract void onTick (long millisUntilFinished)

Added in API level 1
Callback fired on regular interval.
Parameters
millisUntilFinishedThe amount of time until finished.

public final synchronized CountDownTimer start ()

Added in API level 1
Start the countdown.
除了start(),cancel以外

就是onFinish() ->倒數結束以後要幹嘛?

onTick()->每次減少固定單位時間時要做甚麼?

 new CountDownTimer(30000, 1000) {

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

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

CounDownTimer(總共倒數多少秒 , 每次扣多少)

每1000為一秒, 所以30000 = 30 秒 , 1000 = 1秒

範例為倒數30秒,每次扣一秒.

0 意見: