Android

Android 防止畫面連續被點擊

1.快速連點判斷(1s)

private fun isFastDoubleClick(): Boolean {
    val time = System.currentTimeMillis()
    val timeD: Long = time - lastClickTime
    return if (timeD in 0..1000) {
       true
    } else {
        lastClickTime = time
        false
    }
}

2.覆寫dispatchTouchEvent

override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
     if(ev?.action == MotionEvent.ACTION_DOWN) {
         if (isFastDoubleClick()) {
             return true
         }
     }
     return super.dispatchTouchEvent(ev)
}

3.效果展示

發表迴響