Android 如何在单击后立即禁用按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11031814/
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
How to disable button as soon as its clicked
提问by Ali Ashraf
I have a button which on Click loads another activity, but before it loads another Activity if i tap it continuously, then it launches the activity but the same activity is loaded two times or more. I have tried button.setEnabled(false) soon after button.setOnClickListener...onClick but that is not disabling the button, i am also logging a text as soon as the button is tapped, thus depending on my taps it logs the text 2 or 3 times even if i had tapped it 10 times. What i feel here is before the button can listen to tap events, i am tapping it so fast that it listens to all those events as many times as i tap it.
我有一个按钮,点击它会加载另一个活动,但在它加载另一个活动之前,如果我连续点击它,然后它会启动该活动,但相同的活动会加载两次或更多次。我在 button.setOnClickListener...onClick 之后不久尝试过 button.setEnabled(false) 但这并没有禁用按钮,我也在点击按钮后立即记录文本,因此根据我的点击它会记录文本 2或者 3 次,即使我已经点击了 10 次。我在这里的感觉是在按钮可以收听点击事件之前,我点击它的速度如此之快,以至于我点击它时它会收听所有这些事件。
Thus i need something where the button can just listen one tap and then disable it.
因此,我需要一些按钮可以只听一击然后禁用它的东西。
please help
请帮忙
回答by Vipul Shah
Its known issue.Basically u can set the flag.
它的已知问题。基本上你可以设置标志。
int flag=1;
@Override
public void onClick(View v)
{
if(flag)
{
button.setEnabled(false);
Log.d("ins", "called");
}
flag=0;
}
回答by Narasimha
more preferred solution is,
更优选的解决方案是,
onclick(){
btn.setEnabled(false);
btn.setClickable(false);
//yourwork
myWork();
}
myWork(){
//your tasks.
btn.setEnabled(true);
btn.setClickable(true);
}
回答by Rafael T
This can be considered as an android-bug. You are right with your assumption, that the click-event is caught by the system, and may be provided too late, if one clicks continuously on a clickable. You can try to get around this problem by inserting a boolean
value like this
这可以被认为是一个android-bug。您的假设是正确的,即点击事件被系统捕获,并且可能为时已晚,如果连续点击可点击事件。您可以尝试通过插入这样的boolean
值来解决此问题
private boolean handledClick = false;
public void onClick(View v){
if (!handledClick){
handledClick = true;
startActivity(...);
}
}
this will ensure, that even if onClick
is triggered more that once, the Code inside gets executed only once...
这将确保,即使onClick
被多次触发,里面的代码也只执行一次......
回答by Rohit Suthar
Try Button.setEnabled(true/false)
like this :
Button.setEnabled(true/false)
像这样尝试:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.isEnabled()) {
v.setEnabled(false);
}
}
});
You can also check Button
is enabled or not by calling Button.isEnabled()
.
您还可以Button
通过调用来检查是否启用Button.isEnabled()
。
For More about Button
Read This…
回答by Arun George
I too had a similar problem but I was able to disable the button in the onClick()
callback method of the listener by using button.setEnabled(false) of the button. I enable the button at the onResume()
, onRestart()
and onWindowFocusChanged()
of the activity that has the button.
我也有类似的问题,但我能够onClick()
通过使用按钮的 button.setEnabled(false) 在侦听器的回调方法中禁用按钮。我在onResume()
,onRestart()
和onWindowFocusChanged()
具有该按钮的活动中启用按钮。
And I assume this is one of the way to disallow user to tap a button (continuously).
我认为这是禁止用户点击按钮(连续)的一种方法。
回答by ppsreejith
If go refers to the button you need,
如果 go 指的是你需要的按钮,
go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your code here
go.setEnabled(false);
}
});
EDIT: Since it still queues up, how about.
编辑:因为它仍然排队,怎么样。
s.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
s.setEnabled(false);
if(flag == 1){
flag=0;
//your code here
}
}
});
回答by Grant
I also had the same problem and this is how I solved it :
我也遇到了同样的问题,这就是我解决它的方法:
int flag = 1;
private OnClickListener btnNextActivity = new OnClickListener(){
public void onClick(View v) {
if(flag){
Intent nextActivity = new Intent(getApplicationContext(),NextActivity.class);
nextActivity.setClassName(". . .",". . ." );
. . .
startActivity(nextActivity);
flag = 0;
}
else{
Show Toast Message Here
}
}
};