[英]How to use the chronometer as a countdown timer in Android
I have defined a chronometer; 我已经定义了一个天文台表;
protected Chronometer chrono;
protected int baseTime;
protected int stopTime;
protected long elapsedTime;
My program asks questions to the user and i want to set a timer based on the user's input. 我的程序向用户提问,我想根据用户的输入设置一个计时器。 I want a timer which starts at 10 to 0. How can i do that?
我想要一个从10到0开始的计时器。我该怎么做?
Also, I want to show the remaining time on the screen. 另外,我想在屏幕上显示剩余时间。
Use CountDownTImer instead, 请改用CountDownTImer ,
new CountDownTimer(10000, 1000) { //Sets 10 second remaining
public void onTick(long millisUntilFinished) {
mTextView.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextView.setText("done!");
}
}.start();
Note : It's not possible to start Chronometer reversely because the Chronometer widget only counts up. 注意:由于Chronometer小部件仅计数,因此无法反向启动Chronometer。
Edit: From API level 24, it is possible to perform count down using chronometer via help of Chronometer#setCountDown(true)
method. 编辑:从API级别24开始,可以通过
Chronometer#setCountDown(true)
方法使用精密计时器执行倒计时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.