SoFunction
Updated on 2025-04-06

Sample code for continuous clicks of android block buttons

In Android development, there are inevitably many button clicks. In order to prevent users from clicking buttons intentionally, causing too many unnecessary requests in a short time, or the problem of multiple jump pages, the client needs to block the click action, that is, block continuous clicks in a short time. (Of course, this is just to minimize the occurrence of the above problems. If the hand speed is fast enough, it will still occur)

Correct code:

abstract class OnMultiClickListener(private val interval: Long = MULTI_CLICK_INTERVAL):  {
  private companion object {
    private const val MULTI_CLICK_INTERVAL = 500L
  }
 
  private var mLastClickTime = 0L
 
  abstract fun onMultiClick(v: View?)
 
  final override fun onClick(v: View?) {
     {
      val currentTime = ()
      //Note that you should use absolute value here      if (abs(currentTime - mLastClickTime) < interval) {
        mLastClickTime = currentTime //Assign a value        return
      }
  
      mLastClickTime = currentTime //Assign two 
      onMultiClick(v)
 
    }
  }
}

Note:

1. If the interval time is set too long, it may cause visual click lag

2. If the system time is modified, it may cause time judgment problems, which will lead to unresponsive button clicks. Therefore, it must be compared with absolute values.

3. Pay attention to two assignment positions

This is the article about continuously clicking the Android block button. For more related contents related to continuously clicking the Android block button, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!