How to combine OnClickListener and OnTouchListener for an ImageButton

后端 未结 5 570
一整个雨季
一整个雨季 2020-12-10 01:12

I need to do something when the user clicks the ImageButton I\'ve tried to create a static class that implements both OnClickListener

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 01:53

    Since both actions consist of the gestures "put finger on screen - lift finger from screen" you can't determine if it was touch action or a click action. So if you implement both listeners on this image button, a touch/click will change the picture AND press the button. Not sure if there is a determined order of these events...

    However, if you want to separate these events, you will either need to define to a different gesture to one of the actions (like wiping to change picture), or create different areas who handle the events, for example the image doesn't fit the whole button and the free area serves as button click area.

    HTH

    Update:

    I figured out, that a TouchEvent is more general than a ClickEvent thus it is called first.

    public abstract boolean onTouch (View v, MotionEvent event)
    

    This returns true, if the listener has consumed the event, false otherwise. So you can decide in your implementation if the Event should also be handled by OnClickListener, then just return false.

提交回复
热议问题