我在Eclipse中计时器没有正常工作我在、计时器、正常、工作

2023-09-03 22:49:09 作者:吥崾銩丅莪、ぬ庅

我是新来的Eclipse和我在eclipse计时器没有正常工作,这是我的Java。 code。在定时器:

I am new to eclipse and my timer in eclipse did not work properly, this is my java. code in the 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();

        }
    });

基本上一切都运行如我所愿,但是,我发现,如果我点击触发按钮,而它计数,这将触发不停止previous计数另计。这是很尴尬的,我的朋友建议我做的开关,如果再按下此键触发,和我正在考虑到其他行动加入到同一个按钮是停止重新开始计数。哪一个更preferable?

Basically everything is running as I wish, however, I found that if I clicked the trigger button while it is counting, it will trigger another counting without stop the previous counting. This is very embarrassing, my friend suggest me to do "switch" if the button trigger again, and I am considering to add another actions to the same button which is stop the restart counting. Which one is more preferable?

推荐答案

禁用触发按钮。

这样,你将节省的痛苦fiqure如何做一个开关或之后,第一次点击添加其他操作的按钮。

That way you will save the pain to fiqure out how to do a switch or adding other actions to the button after the first click.

您可以尝试 yourbutton.setEnabled(假); 来使残疾人

如果我理解了code正确的,它应该是

if i have understood the code right, it should be

countDownButton2.setEnabled(假);

要再次启用按钮,使用

countDownButton2.setEnabled(真);