How to prvent several clicks in android
class OneClickListener(private val oneClick: (View) -> Unit) : View.OnClickListener {
companion object {
const val CLICK_INTERVAL = 500
}
private var lastClickedTime: Long = 0L
private fun isSafe() : Boolean {
return System.currentTimeMillis() - lastClickedTime > CLICK_INTERVAL
}
override fun onClick(view: View?) {
if(isSafe() && view != null) {
oneClick(view)
}
lastClickedTime = System.currentTimeMillis()
}
}
fun View.setOneClickListener(oneClick: (view: View) -> Unit) {
setOnClickListener(OneClickListener {
oneClick(it)
})
}
댓글 없음:
댓글 쓰기