我需要一些帮助.我无法正确显示分钟数,因为它给0而不是我想要的数字,但第二个工作正常.这是我的代码:
new CountDownTimer(90000, 1000) { @Override public void onTick(long millisUntilFinished) { tvTime.setText("Time remaining: " + String.format("%d min: %d sec", TimeUnit.MICROSECONDS.toMinutes( millisUntilFinished), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))); } });
我希望它在1分钟内显示:30秒而不是0分钟:在倒计时开始时30秒.
试试这个
String text = String.format(Locale.getDefault(), "Time Remaining %02d min: %02d sec", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60, TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60); tvTime.setText(text);
## Try this one ## private void startTimer(int noOfMinutes) { CountDownTimer countDownTimer = new CountDownTimer(noOfMinutes, 1000) { public void onTick(long millisUntilFinished) { long millis = millisUntilFinished; //Convert milliseconds into hour,minute and seconds String hms = String.format("%02d:%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))); countdownTimerText.setText(hms);//set text } public void onFinish() { countdownTimerText.setText("TIME'S UP!!"); //On finish change timer text } }.start(); }