我遇到了一些问题,如果计时器小于10秒,我该如何尝试设置if else条件,它必须提醒用户他们还剩10秒?
以下是更容易参考的代码:
runTimer = new CountDownTimer(45000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
String text = String.format(Locale.getDefault(), "%02d min: %02d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60, TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
tvTime.setText(text);
}
@Override
public void onFinish() {
runTimer.cancel();
}
}.start();
答案 0 :(得分:2)
检查此代码:
runTimer = new CountDownTimer(45000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
String text;
if (millisUntilFinished < 1000)
text = String.format(Locale.getDefault(), "%02d min: %02d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60, TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
tvTime.setText(text);
}
@Override
public void onFinish() {
runTimer.cancel();
}
}.start();
答案 1 :(得分:1)
试试这个:
runTimer = new CountDownTimer(45000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
if (millisUntilFinished / 1000 == 10){
// SHOW MESSAGE HERE
}
}
@Override
public void onFinish() {
runTimer.cancel();
}
}.start();