我是 Eclipse 新手,我的 Eclipse 计时器无法正常工作,这是我的 java.util.Timer 。定时器中的代码:
Button countDownButton2 = (Button) findViewById(R.id.countDown1);
countDownButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
CountDownTimer timer2 = new CountDownTimer(3000,1000){
@Override
public void onFinish() {
mTimeLabel1.setText("Times Up baby!");
}
@Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
mTimeLabel1.setText("" + minutes + ":"
+ String.format("%02d", seconds));
}
}.start();
}
});
基本上一切都按照我的意愿运行,但是,我发现如果我在计数时单击触发按钮,它将触发另一次计数,而不会停止之前的计数。这很尴尬,我的朋友建议我如果按钮再次触发就进行“切换”,并且我正在考虑向同一个按钮添加另一个操作,即停止重新开始计数。哪一个更可取?
定时器运行时禁用触发按钮。
这样,您就可以省去在第一次单击后弄清楚如何进行切换或向按钮添加其他操作的麻烦。
你可以尝试yourbutton.setEnabled(false);
使其禁用。
如果我理解正确的代码,它应该是
countDownButton2.setEnabled(false);
要再次启用该按钮,请使用
countDownButton2.setEnabled(true);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)