kotlin使用扩展函数防止多次点击(重复按)

kotlin使用扩展函数防止多次点击(重复按)

代码:

object ViewClickDelay {
    var hash: Int = 0
    var lastClickTime: Long = 0
    var SPACE_TIME: Long = 3000
}
 
infix fun View.clickDelay(clickAction: () -> Unit) {
    this.setOnClickListener {
        if (this.hashCode() != hash) {
            hash = this.hashCode()
            lastClickTime = System.currentTimeMillis()
            clickAction()
        } else {
            val currentTime = System.currentTimeMillis()
            if (currentTime - lastClickTime > SPACE_TIME) {
                lastClickTime = System.currentTimeMillis()
                clickAction()
            }
        }
    }
}

调用:

tv_test_click.clickDelay {
            Log.d("WY+", "按下")
        }

本文转载:https://blog.csdn.net/weixin_33730836/article/details/91362266

发布了26 篇原创文章 · 获赞 24 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/wy313622821/article/details/105446785