现在我的计时器显示秒数,我可以添加什么来以 0:00 格式显示时间?
new CountDownTimer((300 * 1000), 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("Session Completed!");
}
}.start();
试试这个:
Date date = new Date((300 * 1000)* 1000);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String dateFormatted = formatter.format(date);
您也可以使用
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
@Override
public void onTick(long millisUntilFinished) {
long temp_long = millisUntilFinished / 1000;
second = temp_long % 60;
hour = temp_long / 3600;
minute = (temp_long / 60) % 60;
String tempTimer;
tempTimer = ((hour < 10) ? "0" + hour : "" + hour)+ ((minute < 10) ? ":0" + minute : ":"+ minute)+ ((second < 10) ? ":0" + second : ":" + second);
mTextField.setText(tempTimer);
}
您可以使用 android Chronometer 显示倒计时。
http://developer.android.com/reference/android/widget/Chronometer.html
你可以试试这个
val min:长 = millisUntilFinished / 1000 / 60
val 秒:长 = millisUntilFinished / 1000 % 60
如果(秒< 10) {
binding?.value?.text = "0$min:0$sec"
}其他{
binding?.value.text =" 0$min:$sec"
}