我需要一些帮助。我无法正确显示分钟数,因为它给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秒。
答案 0 :(得分:13)
试试这个
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);
答案 1 :(得分:5)
## 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();
}
答案 2 :(得分:0)
long millisUntilFinished是剩下的毫秒 你可以通过将它除以1000来获得秒数 并且可以通过再次将其除以60来获得分钟。
答案 3 :(得分:0)
int secondsUntilFinished = milisUntilFinished / 1000;
int seconds = secondsUntilFinished % 60;
int mins = secondsUntilFinished / 60;
答案 4 :(得分:0)
CountDownTimer myCountDownTimer = new CountDownTimer(
i, 1000) {
@Override
public void onTick(long millisUntilFinished) {
int time = (int) millisUntilFinished;
int seconds = time / 1000 % 60;
int minutes = (time / (1000 * 60)) % 60;
timeText.setText(twoDigitString(minutes) + " min: "+ twoDigitString(seconds)+ " sec");
}
private String twoDigitString(long number) {
if (number == 0) {
return "00";
} else if (number / 10 == 0) {
return "0" + number;
}
return String.valueOf(number);
}
@Override
public void onFinish() {
timeText.setText("00:00");
}
}
试试这段代码。
答案 5 :(得分:0)
用以下代码替换您的代码:
new CountDownTimer(90000, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
tvTime.setText("Time remaining: "+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
答案 6 :(得分:0)
针对以不同观点面对时,分,秒的人。只需重新使用此代码。它可以减少您的开发时间:)
new CountDownTimer(7200000, 1000) {
@Override
public void onTick(long millis) {
// , , ;
String hours = String.format(Locale.getDefault(), "%02d",
TimeUnit.MILLISECONDS.toHours(millis));
String minutes = String.format(Locale.getDefault(), "%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)));
String seconds = String.format(Locale.getDefault(), "%02d",
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
hour.setText(hours);
minute.setText(minutes);
second.setText(seconds);
}
@Override
public void onFinish() {
hour.setText("00");
minute.setText("00");
second.setText("00");
}
}.start();
结果
答案 7 :(得分:-1)
mCountDownTimer = new CountDownTimer(3600000, 1000) {
public void onTick(long millisUntilFinished) {
String day = TimeUnit.HOURS.toDays(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)) + "";
if (day.equalsIgnoreCase("0")) {
day = "";
} else if (day.equalsIgnoreCase("1")) {
day = day + "day ";
} else {
day = day + "days ";
}
String hr = (TimeUnit.MILLISECONDS.toHours(millisUntilFinished) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millisUntilFinished))) + "";
String min = (TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished))) + "";
String ss = (TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))) + "";
holder.txtDiscount.setText(productBean.getOfferDiscount() + "% OFF" + " Special offer ends in " + day + hr + ":" + min + ":" + ss);
}
public void onFinish() {
holder.txtDiscount.setVisibility(View.GONE);
}
}.start();