满足某些条件时如何执行button.setOnTouchListener(Kotlin)

时间:2020-07-28 13:06:25

标签: java kotlin

我按下了按钮,并为其添加了触摸监听器。

当我按住它时,名为“ fun”的函数将在毫秒内重复执行。

但是我想除了按住按钮外还要添加一个条件。 (就像“ a == true”一样)

当我编写诸如

的代码时
if ( a == true ) { button.setOnTouchListener(RepeatListener(initialInterval, normalInterval, View.OnClickListener {
        fun()
    }))

这是我在此网站上找到的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
    }
}

它不能正常工作。.

如何编写“当a为true时,按住按钮会反复执行fun()” ??

1 个答案:

答案 0 :(得分:1)

写类似的东西有什么问题

button.setOnTouchListener { if (a) fun() }

如果您要实现某种行为,例如“ afalse时,执行一次fun,则也可以尝试添加

button.setOnClickListener { if (a) return else fun() }