//定义有效点击时长
private var clickEffectiveTime = 1000L
//记录上次点击的时间
private var lastClickTime: Long = 0
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 > clickEffectiveTime) {
lastClickTime = System.currentTimeMillis()
clickAction()
}
}
}
}
使用
button clickDelay {
//处理点击按钮的逻辑
}