Android Kotlin onTouchListener希望我覆盖performClick() [英] Android kotlin onTouchListener wants me to override performClick()
问题描述
在Android Studio希望我的onTouchListener覆盖我执行的performClick的情况下,我试图摆脱警告,但该警告仍然存在.
I'm trying to get rid of a warning where Android Studio wants my onTouchListener to override performClick which I do, but the warning remains.
draggableBar!!.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
when (motionEvent.getAction()) {
MotionEvent.ACTION_DOWN -> {
}
MotionEvent.ACTION_UP -> {
view.performClick()
}
}
return@OnTouchListener true
})
这可能是Android Studio的错误,还是我做错了什么?
Could this be an Android Studio bug or am I doing something wrong?
推荐答案
好的,我有同样的问题,但是我通过覆盖onTouch侦听器来解决了.
Okay, I have the same problem but i fixed it by overriding the onTouch listener.
默认的onTouch希望我们覆盖performClick(),但是即使通过view.performClick()调用该方法也无法正常工作.
The default onTouch wants us to override performClick(), but this does not work even calling the method by view.performClick().
因此,请像这样覆盖您的onTouch:
So therefore override your onTouch like this:
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().
And in that way, you can use single onTouch() in all clickable views you have.
别忘了实现您的班级:
View.OnTouchListener
并设置监听器:
draggableBar!!.setOnTouchListener(this)
希望能帮助您! :)
这篇关于Android Kotlin onTouchListener希望我覆盖performClick()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!