What is the best way to prevent double clicks on a button in Android?
Present Code click(function (e) { // Prevent button from double click var isPageValid = Page_ClientValidate(); if (isPageValid) { if (isOperationInProgress == noIndicator) { isOperationInProgress = yesIndicator; } else { e.
You can implement onClickListener in your Activity. Set onClickListener for each button. Then just detect the corresponding button pressed. public class YourActivity extends Activity implements OnClickListener { Button button1, button2; @Override protected void onCreate(Bundle savedInstanceState) { super.
To double-click, press your left mouse button twice quickly.
saving a last click time when clicking will prevent this problem.
i.e.
private long mLastClickTime = 0;
...
// inside onCreate or so:
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// mis-clicking prevention, using threshold of 1000 ms
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
// do your magic here
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With