I made button and give it touchlistener.
When I hold it, function named "fun" is executed repeatedly with milliseconds.
But I want to add a condition in addition to holding button. (Just like "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.
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"??
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
button.setOnClickListener { if (a) return else fun() }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.