Android kotlin onTouchListener 要我覆盖 performClick()

jus*_*ser 7 android kotlin android-studio

我正在尝试消除 Android Studio 希望我的 onTouchListener 覆盖我所做的 performClick 的警告,但警告仍然存在。

draggableBar!!.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
    when (motionEvent.getAction()) {
        MotionEvent.ACTION_DOWN -> {

        }
        MotionEvent.ACTION_UP -> {
            view.performClick()
        }
    }

    return@OnTouchListener true
})
Run Code Online (Sandbox Code Playgroud)

这可能是 Android Studio 的错误还是我做错了什么?

lam*_*bda 4

好的,我有同样的问题,但我通过覆盖 onTouch 侦听器修复了它。

默认的onTouch希望我们重写performClick(),但是即使通过view.performClick()调用该方法也不起作用。

因此,像这样覆盖你的 onTouch :

override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
    when (view) {
        draggableBar -> {
            when (motionEvent.getAction()) {
                MotionEvent.ACTION_DOWN -> {

                }
                MotionEvent.ACTION_UP -> {
                    view.performClick()
                }
            }
        }
        otherButtonHere -> {
            //your welcome
        }
    }

    return true
}
Run Code Online (Sandbox Code Playgroud)

这样,您就可以在所有可点击视图中使用单个 onTouch() 。

不要忘记在您的班级中实施:

View.OnTouchListener
Run Code Online (Sandbox Code Playgroud)

并设置监听器:

draggableBar!!.setOnTouchListener(this)
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!:)