Android: Custom button OnClickListener is not getting invoked

半世苍凉 提交于 2019-12-04 07:19:04
Tom Clabon

In your onTouchEvent implementation, instead of "return true;", do...

return super.onTouchEvent(event);

You're overriding the superclass' implementation which is what is responsible for calling the listener. By calling the superclass' implementation it should act as it did before. This is why your code works when you comment out the method - because you're no longer overriding the superclass' implementation

Try impelementing OnTouchListener in your activity (instead of onClickListener) and change onClick() to onTouch(). This worked for me. Both onTouchEvent from my custom view and onTouch() from Activity are being called. Remeber to return "false" in onTouch() and "true" in OnTouchEvent of your custom view.

I guess Mathias's comment is correct, you have to return false in your onTouchEvent method when you want the onClick() event listener to be triggered instead of a subsequent onTouch() event listener.

You can find more precision int UI Events documentation

Can you just execute your code in OnTouchEvent (a click is MotionEvent.ACTION_DOWN as you already know)?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!