[英]How to execute button.setOnTouchListener when some conditions are met (Kotlin)
I made button and give it touchlistener.我制作了按钮并给它 touchlistener。
When I hold it, function named "fun" is executed repeatedly with milliseconds.当我按住它时,名为“fun”的function以毫秒为单位重复执行。
But I want to add a condition in addition to holding button.但是除了按住按钮之外,我还想添加一个条件。 (Just like "a == true") (就像“a == true”)
when I write the code such as当我写代码时
if ( a == true ) { button.setOnTouchListener(RepeatListener(initialInterval, normalInterval, View.OnClickListener {
fun()
}))
and here is RepeatListener I found at this site.这是我在这个网站上找到的 RepeatListener。
import android.os.Handler
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
/**
* A class, that can be used as a TouchListener on any view (e.g. a Button).
* It cyclically runs a clickListener, emulating keyboard-like behaviour. First
* click is fired immediately, next one after the initialInterval, and subsequent
* ones after the normalInterval.
*
*
* Interval is scheduled after the onClick completes, so it has to run fast.
* If it runs slow, it does not generate skipped onClicks. Can be rewritten to
* achieve this.
*/
class RepeatListener(initialInterval: Long, normalInterval: Long, clickListener: View.OnClickListener?) : OnTouchListener {
private val handler = Handler()
private val initialInterval: Long
private val normalInterval: Long
private val clickListener: View.OnClickListener
private var touchedView: View? = null
private val handlerRunnable: Runnable = object : Runnable {
override fun run() {
if (touchedView!!.isEnabled) {
handler.postDelayed(this, normalInterval)
clickListener!!.onClick(touchedView)
} else {
// if the view was disabled by the clickListener, remove the callback
handler.removeCallbacks(this)
touchedView!!.isPressed = false
touchedView = null
}
}
}
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
handler.removeCallbacks(handlerRunnable)
handler.postDelayed(handlerRunnable, initialInterval)
touchedView = view
touchedView!!.isPressed = true
clickListener.onClick(view)
return true
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
handler.removeCallbacks(handlerRunnable)
touchedView!!.isPressed = false
touchedView = null
return true
}
}
return false
}
/**
* @param initialInterval The interval after first click event
* @param normalInterval The interval after second and subsequent click
* events
* @param clickListener The OnClickListener, that will be called
* periodically
*/
init {
requireNotNull(clickListener) { "null runnable" }
require(!(initialInterval < 0 || normalInterval < 0)) { "negative interval" }
this.initialInterval = initialInterval
this.normalInterval = normalInterval
this.clickListener = clickListener
}
}
it didn't work properly..它没有正常工作..
How to write "when a is true, holding button excutes fun() repeatedly"??如何写“当 a 为真时,按住按钮重复执行 fun()”?
What is the problem with writing something like写类似的东西有什么问题
button.setOnTouchListener { if (a) fun() }
If you want to achieve behavior, such as "when a
is false
, execute fun
once, you can also try adding如果你想实现行为,比如“当a
为false
时,执行一次fun
,你也可以尝试添加
button.setOnClickListener { if (a) return else fun() }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.