简体   繁体   English

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

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

How do I make a button unclickable for a period of time?如何让按钮在一段时间内无法点击?

Ask for more information if needed.如果需要,请询问更多信息。

This is on android.这是在 android 上。

if you want unclickable Button then you can use Button.setEnabled(false);如果你想要不可点击的按钮,那么你可以使用Button.setEnabled(false);

Untested Code未经测试的代码

You can use a handler .您可以使用handler The given code make your button unclickable for 1.5 seconds .给定的代码使您的按钮在 1.5 秒内无法点击 and then re enable it.然后重新启用它。

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

You can use TimerTask which runs at a specified time repeaditly or once .您可以使用在指定时间重复一次运行的TimerTask Please refer Updating the UI from a Timer document.请参阅从 Timer 文档更新 UI

You can first disable your button with setEnabled(false) and use postDelayed to enable it again after a specific period of time您可以先使用setEnabled(false)禁用按钮,然后在特定时间段后使用postDelayed再次启用它

An interesting thing you could do is use an ObjectAnimator and animate an int value from 0 to 1. I'll try this later.您可以做的一件有趣的事情是使用ObjectAnimator并将 int 值从 0 设置为 1。我稍后会尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM