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();
}
}
}
我遇到的问题是我提供的代码的底部。我在论坛中找到的一些代码,它有一个我无法解决的问题。我的计时器始终从25:0
开始,然后进入24:59
。这也会在显示器中稍后出现,例如:23:0
。我试图使秒始终显示两位数而不是一个0。
答案 0 :(得分:1)
这可以通过DecimalFormat完成:
// 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
或作为替代用途String.format():
// 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
答案 1 :(得分:0)
使用String.format()对您的seconds
进行零填充,以确保它始终为两位数:
timerText.setText(minutes + ":" + String.format("%02d", seconds));
答案 2 :(得分:0)
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(2.0));
输出将为2.00
这对您来说更容易嵌入。