我正在研究Android应用程序,其中我想显示倒计时Android计时器的唯一秒数。例如仅持续10秒。我的代码如下:
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
答案 0 :(得分:1)
long millis = millisUntilFinished;
String secondsOnly = String.format("%02d", millis / 1000);
就是这样。如果您不希望它超过60,请使用:
long millis = millisUntilFinished;
String secondsOnly = String.format("%02d", (millis / 1000) % 60);
答案 1 :(得分:0)
只需检查以下代码,它就会100%帮助您。
new CountDownTimer(30000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("done!");
}
}
.start();