使用倒计时设置按钮启用倒计时、按钮

2023-09-11 05:00:38 作者:掌心dē余温

我试图避免反复接触,但希望有一个敏感的屏幕。使用这个答案通知的方法:

I am trying to avoid repeated touches, but want a sensitive screen. Using the method advised in this answer:

的Andr​​oid preventing双点击一个按钮

我已经重新启用按钮的程序流程中,但也有一些情况下,我不能靠程序流程,并需要确保启用按钮。

I have been re-enabling buttons within the program flow, but there are some instances where, I cannot rely on program flow and need to ensure the button is enabled.

我设计了一个方法,使用重新启用这些按钮 countdowntimer 在我的code所示:

I have devised a way to re-enable these button using a countdowntimer and shown in my code:

button.setOnTouchListener(new View.OnTouchListener() {
        @Override public boolean onTouch(View v, MotionEvent event) {
            disableButton(button);
            countDwn();
            // Do something
            return false;
        }
    });

public void disableButton(Button button) {
    button.setEnabled(false);
}

public void enableButton(Button button) {
    button.setEnabled(true);
}

public void countDwn() {
    CountDownTimer countDownTimer = new CountDownTimer(2000, 1000) {
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
            enableButton(button);
        }
    }.start();
}

我就是关心,是,这可能是一个笨拙的或无效的方式来进行此事。我想咨询一下这一点,如果任何人有一个更优雅的建议?

What I am concerned about, is that this may be a clumsy or ineffective way to go about this. I am wanting advice about this and if anyone has a more elegant suggestion?

推荐答案

我不知道设置一个按钮启用=假是解决反复触摸问题的正确途径。

I am not sure setting a button to enable=false is the correct way to solve the "repeated touches" issue.

主要的原因是因为当一个按钮启用=假,大多数时间它都会有残疾人图形​​分配给它。因为我们只想要prevent意外反复触摸,而不是调用禁用的图形,我不知道这是正确的解决方案。

The main reason for that is since when a button is enable=false, most of the time it will have a Disabled graphics assigned to it. Since we only want to prevent accidental repeated touches, and not to invoke the disable graphics, I am not sure this is the correct solution.

prevent反复触摸

我会建议一个简单的解决方案,prevent的行动,如果从最后一个动作的时间小于 MINIMUM_ACTION_DELAY

I will suggest a simpler solution, prevent the action if the time from last action is less than MINIMUM_ACTION_DELAY.

。如果您不想点击动画,prevent的onTouch的动作。

If you want to get the click animation, prevent the action on the onClick listener. If you don't want the click animation, prevent the action on the onTouch.

例如,会的onClick是这样的:

For example, onClick will be something like this:

button.setOnClickListener(new View.OnClickListener() {

    private long mLastActionTime;

    @Override
    public void onClick(View v) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - mLastActionTime < MINIMUM_ACTION_DELAY) {
            // Too soon, we don't want to handle this event
            return;
        }

        // Save the action time
        mLastActionTime = currentTime;

        // Execute action
        // TODO do something
    }
});