How to disable button as soon as its clicked

2019-01-24 16:19发布

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.

Thus i need something where the button can just listen one tap and then disable it.

please help

7条回答
做个烂人
2楼-- · 2019-01-24 16:38

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

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...

查看更多
做自己的国王
3楼-- · 2019-01-24 16:45

more preferred solution is,

onclick(){
  btn.setEnabled(false);
  btn.setClickable(false);
  //yourwork
  myWork();
}

myWork(){
 //your tasks.
 btn.setEnabled(true);
 btn.setClickable(true);
}
查看更多
小情绪 Triste *
4楼-- · 2019-01-24 16:49

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;
            }
查看更多
等我变得足够好
5楼-- · 2019-01-24 16:49

Try Button.setEnabled(true/false) like this :

    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().


For More about Button Read This…

查看更多
6楼-- · 2019-01-24 16:51

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.

And I assume this is one of the way to disallow user to tap a button (continuously).

查看更多
男人必须洒脱
7楼-- · 2019-01-24 16:52

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
              }
      }
};
查看更多
登录 后发表回答