Android的kotlin onTouchListener要我重写performClick()

我试图摆脱Android Studio要求我的onTouchListener重写performClick,我做的警告,但警告仍然存在。

draggableBar!!.setOnTouchListener(View.OnTouchListener { view, motionEvent -> when (motionEvent.getAction()) { MotionEvent.ACTION_DOWN -> { } MotionEvent.ACTION_UP -> { view.performClick() } } return@OnTouchListener true }) 

这可能是一个Android Studio错误,或者我做错了什么?

好吧,我有同样的问题,但我通过覆盖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 } 

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

不要忘记实施你的课堂:

 View.OnTouchListener 

并设置监听器:

 draggableBar!!.setOnTouchListener(this) 

希望它帮助! 🙂