Skip to content

Instantly share code, notes, and snippets.

@yava555
Last active May 29, 2021 00:42
Show Gist options
  • Save yava555/f05e952ca9d2c37494f86c0dd54efb0d to your computer and use it in GitHub Desktop.
Save yava555/f05e952ca9d2c37494f86c0dd54efb0d to your computer and use it in GitHub Desktop.
Prevent double click
import android.view.View;
public abstract class OneClickListener implements View.OnClickListener {
private long lastClickTime = 0;
private static long DIFF = 1000;
@Override
public void onClick(View v) {
if (!isFastDoubleClick()) {
doClick(v);
}
}
/**
* Determine whether double-click quickly
*
* @return
*/
public boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if (lastClickTime > 0 && timeD < DIFF) {
return true;
}
lastClickTime = time;
return false;
}
public abstract void doClick(View v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment