Kotlin OnTouchListener已调用,但未覆盖performClick [英] Kotlin OnTouchListener called but it does not override performClick

查看:163
本文介绍了Kotlin OnTouchListener已调用,但未覆盖performClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在科特林中覆盖performClick以避免警告?

How to override performClick in Kotlin to avoid warning?

next.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
        when (motionEvent.action){
            MotionEvent.ACTION_DOWN -> {
                val icon: Drawable = ContextCompat.getDrawable(activity.applicationContext, R.drawable.layer_bt_next)
                icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY)
                next.setImageDrawable(icon)
            }
            MotionEvent.ACTION_UP -> {
                //view.performClick()
                next.setImageResource(R.drawable.layer_bt_next)
            }
        }
        return@OnTouchListener true
    })

view.performClick不起作用.

推荐答案

好吧,我通过重写OnTouch侦听器解决了自己的问题.

Okay, I solved my own problem by overriding the OnTouch listener.

override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
    when (view) {
        next -> {
            Log.d("next", "yeyy")
            when (motionEvent.action){
                MotionEvent.ACTION_DOWN -> {
                    val icon: Drawable = ContextCompat.getDrawable(activity.applicationContext, R.drawable.layer_bt_next)
                    icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY)
                    next.setImageDrawable(icon)
                }
                MotionEvent.ACTION_UP -> {
                    view.performClick()
                    next.setImageResource(R.drawable.layer_bt_next)
                }
            }
        }
        previous -> {
            //ingredients here XD
        }
    }
    return true
}

这样,我可以调用单个onTouch并将其实现为多个按钮,也可以使用onClick by:

And in that way, I can call single onTouch and implement it to many button and also can use the onClick by :

view.performClick()

别忘了实现:

View.OnTouchListener

并设置监听器:

next.setOnTouchListener(this)
previous.setOnTouchListener(this)

这篇关于Kotlin OnTouchListener已调用,但未覆盖performClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆