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

huangapple go评论76阅读模式
英文:

How to execute button.setOnTouchListener when some conditions are met (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

class RepeatListener(initialInterval: Long, normalInterval: Long, clickListener: View.OnClickListener?) : OnTouchListener {
    // ...
}

但它工作得不正常...

如何编写代码以实现 "当 a 为真时,按住按钮会重复执行 fun()"?

英文:

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" ??

答案1

得分: 1

以下是翻译好的内容:

问题在于像下面这样编写代码存在什么问题:

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

如果你想实现这样的行为,即“当 afalse 时,执行一次 fun”,你还可以尝试添加以下代码:

button.setOnClickListener { if (a) return else 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

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

huangapple
  • 本文由 发表于 2020年7月28日 21:06:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63134866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定