我正在尝试做各种各样的猜谜游戏.问题是我的计时器在回答问题(按下按钮)并启动新计时器后流入下一个计时器.这导致两个定时器以不同的间隔改变文本视图,这不是它应该如何.我想知道如何停止我之前的倒计时并开始一个新的倒计时.谢谢!这是我的代码:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final TextView textic = (TextView) findViewById(R.id.button1);
long total = 30000;
final CountDownTimer Count = new CountDownTimer(total, 1000) {
public void onTick(long millisUntilFinished) {
textic.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
textic.setText("OUT OF TIME!");
finish();
}
};
Count.start();
解决方法:
我没有测试代码,但我会使用这样的东西:
final TextView textic = (TextView) findViewById(R.id.button1);
final android.os.CountDownTimer Count = new android.os.CountDownTimer(total, 1000) {
public void onTick(long millisUntilFinished) {
textic.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
textic.setText("OUT OF TIME!");
}
};
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Count.cancel();
Count.start();
}
});