Android prevent clicking too fast

            When users use Android applications, they often click the same button too quickly and multiple times . On the one hand, this is because the application or mobile phone is currently stuck, and on the other hand, it may be because many applications do not set the button click time. Selector or other button response methods (for example, when the button is clicked, the button is enlarged, which is common in games), causing users to mistakenly believe that the current button has not been clicked. Of course, in addition to optimizing the application and setting the click selector, we can also do Some other work, for example, the onClick event of the judgment button only responds once within the specified event segment (in the search function of the forum, we often see the setting that a search can be performed every 10 seconds, which reduces to a certain extent. Invalid network traffic, reduce server pressure, APP is the same), as shown in the following code:

public final class AppUtils {
     private AppUtils () {

    }

    private static long mLastClickTime ; // User judges the time of multiple clicks
 public static boolean isFastDoubleClick () {
         long time = System. currentTimeMillis () ;
         if (Math. abs (time - mLastClickTime ) < 500 ) {
             return true;
 }
         mLastClickTime = time ;
         return false;
 }
                
}



btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AppUtils.isFastDoubleClick()){
                    // 进行点击事件后的逻辑操作
                }
            }
        });



another way. . . . Create a new onclicklistener

public abstract class OnMultiClickListener implements View.OnClickListener{
    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private static long lastClickTime;

    public abstract void onMultiClick(View v);

    @Override
    public void onClick(View v) {
        long curClickTime = System.currentTimeMillis();
        if((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            // 超过点击间隔后再将lastClickTime重置为当前点击时间
            lastClickTime = curClickTime;
            onMultiClick(v);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
btn.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                // 进行点击事件后的逻辑操作
            }
        });



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324602154&siteId=291194637