简体   繁体   中英

Android: Prevent multiple onClick events on a button (that has been disabled)

A button triggers an action that should only be invoked once. The button is disabled and hidden in the onClick handler before the action is performed:

someButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        someButton.setEnabled(false);
        someButton.setClickable(false);
        someButton.setVisibility(View.GONE);
        performTaskOnce();
        }
    });

private void performTaskOnce() {
    Log.i("myapp", "Performing task");
    //Do something nontrivial that takes a few ms (like changing the view hierarchy)
}

Even though the button is disabled immediately, it is nonetheless possible to trigger multiple "onClick" events by tapping multiple times very quickly. (ie performTaskOnce is called multiple times). Is seems that the onClick events are queued before the the button is actually disabled.

I could fix the problem by checking in every single onClick handle whether the corresponding button is already disabled but that seems like a hack. Is there any better way to avoid this issue?

The problem occurs on Android 2.3.6, I cannot reproduce it on Android 4.0.3. But given the rarity of 4.x devices it is not an option to exclude older devices.

You could set a boolean variable to true when the button is clicked and set it to false when you're done processing the click.

This way you can ignore multiple clicks and not having to disable the button possibly avoiding annoying flickering of the button.

boolean processClick=true;
someButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(processClick)
         {
        someButton.setEnabled(false);
        someButton.setClickable(false);
        someButton.setVisibility(View.GONE);
        performTaskOnce();
         }
        processClick=false; 
        }
    });

private void performTaskOnce() {
    Log.i("myapp", "Performing task");
    //Do something nontrivial that takes a few ms (like changing the view hierarchy)
}

In the interest of keeping DRY :

// Implementation
public abstract class OneShotClickListener implements View.OnClickListener {
    private boolean hasClicked;

    @Override public final void onClick(View v) {
        if (!hasClicked) {
            onClicked(v);
            hasClicked = true;
        }
    }

    public abstract void onClicked(View v);
}

// Usage example
public class MyActivity extends Activity {
    private View myView;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myView.setOnClickListener(new OneShotClickListener() {
            @Override public void onClicked(View v) {
                // do clicky stuff
            }
        });
    }
}

Bit late but this might be of use to someone. In my case I am calling another activity so;

Declare a boolean;

boolean clickable;

In the click listener;

if(clickable){
   // Launch other activity
   clickable = false;
}

Enable when onResume is called;

@Override
public void onResume() {
    Log.e(TAG, "onResume");
    super.onResume();
    clickable = true;
}

You can use RxView ( com.jakewharton.rxbinding2.view.RxView ) is an extension around RxJava that created by Jake Wharton.

To integrate it to project you should use implementation 'com.jakewharton.rxbinding3:rxbinding:3.1.0'

Simple Java usage:

RxView.clicks(yourButton)
    .sample(500, TimeUnit.MILLISECONDS)
    .subscribe { action() }

In Kotlin you can create extension function to handle your clicks:

View.singleClick(action: () -> Any) {
    RxView.clicks(this)
        .sample(500, TimeUnit.MILLISECONDS)
        .subscribe { action() }
}

Sample:

Kotlin

yourButton.singleClick({
    //do some stuff here
})

Java

SingleClickListenerKt.singleClick(yourButton, () -> {
    doSomeStuff();
    return null;
    });

Note: you can use any RxJava operators like debounce, map, first , etc if you wish.

declare a varieble and use it as

 boolean boo = false;
 someButton.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 if(boo==false){
    someButton.setEnabled(false);
    someButton.setClickable(false);
    someButton.setVisibility(View.GONE);
    boo = true;
 }


    }
});

by this you prevent multiple clicks on your button

hope it help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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