无法理解禁用按钮的行为。

huangapple go评论100阅读模式
英文:

Can't understand disable button behavior

问题

以下是翻译好的内容:

我对安卓和Java中的计时概念都不太了解。我试图创建一个简单的应用,在五秒内计算用户在屏幕上的点击次数。时间结束后,我想要禁用按钮,并且在点击“重新开始”按钮时重新启动一切。

以下是当前的代码:

public void clickCounter(View view){
    ++counter;
    if(showCounter!=null)
        showCounter.setText(Integer.toString(counter));
}

public void clickTimer(final View view) {
   final Button Tap = findViewById(R.id.Tap);
   clickCounter(view);
    new CountDownTimer(5000, 5000) {
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
          Tap.setEnabled(false);
        }
    }.start();
}

public void restartCounter(View view) {
    counter=0;
    showCounter.setText("Tap");
    final Button Tap = findViewById(R.id.Tap);
    Tap.setEnabled(true);
}

按钮在5秒后会被禁用,但重新开始按钮有时会使其启用,然后立即禁用(计数器和文本更改正确)。

我认为问题可能出在我使用计时器的方式上(也许应该使用线程?)。

英文:

I'm new to android and pretty new to the concept of timing in Java also. I've tried to create a simple app that counts the number of user clicks on the screen in five seconds. After the time ends, I want to disable the button and restart everything when clicking the 'Restart' button.

This is the current code:

public void clickCounter(View view){
    ++counter;
    if(showCounter!=null)
        showCounter.setText(Integer.toString(counter));
}
public void clickTimer(final View view) {
   final Button Tap = findViewById(R.id.Tap);
   clickCounter(view);
    new CountDownTimer(5000, 5000) {
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
          Tap.setEnabled(false);
        }
    }.start();
        }


public void restartCounter(View view) {
    counter=0;
    showCounter.setText("Tap");
    final Button Tap = findViewById(R.id.Tap);
    Tap.setEnabled(true);
}

The button does disable after 5 seconds, but the restart button sometimes enables and then disables it right away (the counter and text changes properly).

I think the problem might be the way I'm using the Timer to do it (maybe I should use threads?)

答案1

得分: 2

你每次点击按钮都在创建一个新的定时器。因此,当你重新启用按钮时,你的定时器中可能有一个即将到期,并会随后禁用该按钮。只有在没有定时器正在运行时,你才应该创建一个新的定时器。

英文:

You are creating a new timer on every button tap. So, when you reenable the button, one of your timers could be expiring, disabling the button afterwards. You should only create a new timer if there is none running.

答案2

得分: 1

为了扩展Tiago Loureino出色的回答,问题在于每次调用clickTimer()方法时,您都在创建一个新的CountDownTimer对象。

按钮在5秒后会被禁用,但重新启动按钮有时会在启用后立即禁用它(计数器和文本更改正确)。

这种情况发生是因为您有几个CountDownTimer正在执行它们的onFinish()方法。

解决这个问题的方法是拥有一个CountDownTimer的单一实例。在代码中可以这样声明您的CountDownTimer:

public CountDownTimer cdt = new CountDownTimer(5000, 5000) {
    public void onTick(long millisUntilFinished) {
    }

    public void onFinish() {
        Tap.setEnabled(false);
    }
};

然后您可以在任何时候调用cdt.start()来启动计时器。

英文:

To expand on Tiago Loureino's awesome answer, the problem is that you are creating a new CountDownTimer object every time the clickTimer() method is called.

> The button does disable after 5 seconds, but the restart button sometimes enables and then disables it right away (the counter and text changes properly).

This 👆🏽 happens because you have several CountDownTimers executing their onFinish() method.

The solution to this problem is to have one single instance of your CountDownTimer. To put that in code, you can declare your CountDownTimer as below:

public CountDownTimer cdt = new CountDownTimer(5000, 5000) {
    public void onTick(long millisUntilFinished) {
    }

    public void onFinish() {
            Tap.setEnabled(false);
    }
};

You can then call cdt.start() anytime you want to start your the timer.

huangapple
  • 本文由 发表于 2020年10月10日 23:36:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64295190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定