java 第一次点击后禁用按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26168225/
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-11-02 09:25:40  来源:igfitidea点击:

Disable button after first click

javaandroid

提问by BarcelonaDev

I'm making a game, which include two activities.
I have one

我正在制作一个游戏,其中包括两个活动。
我有一个

static class ModelGetter: public static int getPoint{int static point++;}.

static class ModelGetter: public static int getPoint{int static point++;}.

When click a button in the first activity, the counter is incremented How can I avoid that in the same activity if I press twice the same button the counter don't incremente a counter two time, but just one?

当在第一个活动中单击一个按钮时,计数器会增加如果我在同一个活动中按两次相同的按钮,我如何避免这种情况发生,计数器不会将计数器增加两次,而只会增加一次?

回答by GVillani82

Simple: inside the onClick, you need to disable the button:

简单:在 里面onClick,您需要禁用按钮:

yourButton.setEnabled(false); 

So, when you retry to click it, nothing will happen, since the button is now disabled.

因此,当您重试单击它时,什么也不会发生,因为该按钮现在已禁用。

The comple code will be:

完整的代码将是:

yourButton.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                v.setEnabled(false);
                // increment what you want, or other stuff..
            }
        });

回答by Moni

you can add onclick handler like -

您可以添加 onclick 处理程序,例如 -

document.getElementById("buttonId").onclick = function() {

    this.disabled = true; // it will disable your button

    //do whatever your logic here or increment your value
}

or you can call function on onclick of button like -

或者您可以在单击按钮时调用函数,例如 -

function clickButton(){

getElementById("buttonId").disabled=true;

//do whatever your logic here or increment your value

}