In order to prevent the test girl or user from frequently clicking a button, the program will cause multiple data submissions or data processing in a short period of time, which will be more pitiful at that time~
So how to effectively avoid this situation?
My idea is,Determine the interval between users clicking buttons. If the interval is too short, it is considered invalid operation, otherwise related business processing will be carried out.
First extract this piece as a tool class (to facilitate the next call), and now it is named: ButtonUtils
public class ButtonUtils { private static long lastClickTime = 0; private static long DIFF = 1000; private static int lastButtonId = -1; /** * Determine the interval between two clicks. If it is less than 1000, it is considered to be invalid clicks. * * @return */ public static boolean isFastDoubleClick() { return isFastDoubleClick(-1, DIFF); } /** * Determine the interval between two clicks. If it is less than 1000, it is considered to be invalid clicks. * * @return */ public static boolean isFastDoubleClick(int buttonId) { return isFastDoubleClick(buttonId, DIFF); } /** * Determine the interval between two clicks. If it is less than diff, it is considered to be multiple invalid clicks. * * @param diff * @return */ public static boolean isFastDoubleClick(int buttonId, long diff) { long time = (); long timeD = time - lastClickTime; if (lastButtonId == buttonId && lastClickTime > 0 && timeD < diff) { ("isFastDoubleClick", "Button triggers multiple times in a short time"); return true; } lastClickTime = time; lastButtonId = buttonId; return false; } }
We determine whether the current click operation is a valid operation by judging the interval between the two clicks. So how do I call it in use? Keep reading. . .
gv_isf.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if (!(.gv_integralstore)) { // Just write your related operations } } });
!(.gv_integralstore): This is the key. My idea is to make a judgment in the click event to see if the current click event is a valid click event
OK, a simple and practical tool class that prevents buttons from clicking repeatedly will be done. . .
The above article on Android's effective method to prevent repeated clicks of buttons (must-read) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.