java 如何使按钮在一段时间内无法点击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6743504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:06:21  来源:igfitidea点击:

How do I make a button unclickable for a period of time?

javaandroid

提问by Drake

How do I make a button unclickable for a period of time?

如何使按钮在一段时间内无法点击?

Ask for more information if needed.

如果需要,请询问更多信息。

This is on android.

这是在安卓上。

回答by Niranj Patel

if you want unclickable Button then you can use Button.setEnabled(false);

如果你想要不可点击的按钮,那么你可以使用Button.setEnabled(false);

回答by Onuray Sahin

You can use TimerTaskwhich runs at a specified time repeaditlyor once. Please refer Updating the UI from a Timerdocument.

您可以使用TimerTaskwhich 在指定时间重复一次运行。请参阅从计时器文档更新 UI

回答by Jawad Zeb

Untested Code

未经测试的代码

You can use a handler. The given code make your button unclickable for 1.5 seconds. and then re enable it.

您可以使用一个handler. 给定的代码使您的按钮在 1.5 秒内无法点击。然后重新启用它。

int Delay = 1500; //1.5 seconds
yourView.setEnabled(false);// for making the button grey and unclickable

new Handler().postDelayed(new Runnable()
{
    public void run() 
    {  
        yourView.setEnabled(true);
    }
},Delay);

Second Method

第二种方法

//initiate the button
 button.setEnabled(true);
 button.invalidate(); 
 // delay completion till animation completes
 button.postDelayed(new Runnable() {  //delay button 
     public void run() {  
        button.setEnabled(false);
        button.invalidate();
        //any other associated action
     }
 }, 800);  // .8secs delay time

回答by ccheneson

You can first disable your button with setEnabled(false)and use postDelayedto enable it again after a specific period of time

您可以先禁用按钮,setEnabled(false)然后使用postDelayed在特定时间段后再次启用它

回答by Ian

An interesting thing you could do is use an ObjectAnimatorand animate an int value from 0 to 1. I'll try this later.

您可以做的一件有趣的事情是使用 anObjectAnimator并为从 0 到 1 的 int 值设置动画。我稍后会尝试这样做。