如何在android中显示计时器 [英] How to display the timer in android
本文介绍了如何在android中显示计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 TextView 中以类似 [19:59] 的格式显示计时器.所以当我点击开始按钮时,计时器将显示如下,例如,我想设置最多 20 分钟,它会显示像 [19:58][19:87].谁能给出一些想法或示例代码?
I want to display the timer in TextView in the format of like [ 19:59].so when i click the start button ,the timer will display like this for example,i want to set upto 20 mintues,it will display like [19:58][19:87].can anyone give some ideas or example code?
推荐答案
您可以使用CountDownTimer
.
http://developer.android.com/reference/android/os/CountDownTimer.html
TextView _tv = (TextView) findViewById( R.id.textView1 );
new CountDownTimer(20*60000, 1000) {
public void onTick(long millisUntilFinished) {
_tv.setText("seconds remaining: " +new SimpleDateFormat("mm:ss:SS").format(new Date( millisUntilFinished)));
}
public void onFinish() {
_tv.setText("done!");
}
}.start();
要取消只需在计时器上调用取消即可.
To cancel just call cancel on the timer.
public final void cancel()
取消倒计时.
这篇关于如何在android中显示计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文