Android 倒计时计时器显示25:0而不是25:00
我遇到的问题在我提供的代码的底部。这是我在论坛上找到的一些代码,它有一个我无法解决的问题。我的计时器总是从Android 倒计时计时器显示25:0而不是25:00,android,timer,Android,Timer,我遇到的问题在我提供的代码的底部。这是我在论坛上找到的一些代码,它有一个我无法解决的问题。我的计时器总是从25:0开始,然后转到24:59。稍后在显示屏上也会出现这种情况,例如:23:0。我试图使秒始终显示两位数字,而不是一个0。使用将秒归零,以确保秒始终是两位数字: public class MainActivity extends AppCompatActivity { @Bind(R.id.timerText) TextView timerText; @Bind(R.id.p
25:0开始,然后转到24:59
。稍后在显示屏上也会出现这种情况,例如:23:0
。我试图使秒始终显示两位数字,而不是一个0。使用将秒归零,以确保秒始终是两位数字:
public class MainActivity extends AppCompatActivity {
@Bind(R.id.timerText)
TextView timerText;
@Bind(R.id.pauseButton)
ImageButton pauseButton;
@Bind(R.id.restartButton)
ImageButton restartButton;
@Bind(R.id.startButton)
ImageButton startButton;
private CountDownTimer mCounter;
long millisecondsLeft;
boolean isFirstLaunch = true;
boolean isPaused = false;
boolean isRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFirstLaunch) {
pauseButton.setVisibility(View.VISIBLE);
restartButton.setVisibility(View.VISIBLE);
startButton.setVisibility(View.GONE);
isFirstLaunch = false;
setTheTimer(60 * 25 * 1000);
} else {
isRunning = true;
isPaused = false;
changeVisibility();
setTheTimer(millisecondsLeft);
}
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCounter.cancel();
mCounter = null;
isRunning = false;
isPaused = true;
changeVisibility();
}
});
restartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
restart();
}
});
}
public void changeVisibility () {
if (isPaused) {
startButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.GONE);
}
if (isRunning) {
pauseButton.setVisibility(View.VISIBLE);
startButton.setVisibility(View.GONE);
}
}
public void restart () {
if (mCounter != null) {
mCounter.cancel();
mCounter = null;
isRunning = true;
isPaused = false;
changeVisibility();
setTheTimer(60 * 25 * 1000);
} else {
isRunning = true;
isPaused = false;
changeVisibility();
setTheTimer(60 * 25 * 1000);
}
}
private void setTheTimer(long value) {
if (mCounter == null) {
mCounter = new CountDownTimer(value, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long minutes = (millisUntilFinished / 1000) / 60;
int seconds = (int) ((millisUntilFinished / 1000) % 60);
millisecondsLeft = millisUntilFinished;
timerText.setText(minutes + ":" + seconds);
}
@Override
public void onFinish() {
timerText.setText("Done!");
}
};
mCounter.start();
}
}
}
输出将为2.00
这更易于嵌入。这可以通过以下方式实现:
或作为替代用途:
你在使用数字。但是你需要字符串00
不是一个数字;因此,翻译成字符串的0
-是“0”
,而不是“00”
。如何在代码中实现DecimalFormat?我不知道该怎么做,这个答案可能会有所帮助。
timerText.setText(minutes + ":" + String.format("%02d", seconds));
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(2.0));
// modified from your above to highlight what you need to do
NumberFormat f = new DecimalFormat("00");
int seconds = (int) ((millisUntilFinished / 1000) % 60);
timerText.setText(minutes + ":" + f.format(seconds)); // formatter used here
// modified from your above to highlight what you need to do
String format = "%02d"; // two digits
timerText.setText(minutes + ":" + String.format(format, time)); // formatter used here