Difference between Click and Touch Listeners in Android

后端 未结 4 549
暗喜
暗喜 2021-02-07 03:11

I have a bit of doubt. I am using an image button (e.g. Play icon in media player). I want to know which action Listener I am supposed to use, onClickListener or onTouchListener

4条回答
  •  情话喂你
    2021-02-07 03:51

    The answer by @vishy1618 has the key insight of this thread (tried to leave this as a comment there, but too long).

    Conceptually, onClick is just a 'wrapper' around a particular sequence of touch events - down, no drag, up. So comparing onTouch vs. onClick is just a low-level API (raw touch events) vs. a high-level API (a logical user 'click').

    But, an important compatibility issue: in Android, onClick can also be fired by the KEYBOARD (or trackball, or whatever alternative input/hardware device is being used). But (afaict) there's no support for firing touch events via any other input device apart from the touch screen.

    So, if you code your UI against touch events exclusively, you are implicitly requiring a touchscreen. Whereas if you stick to onClick, your app could theoretically work on a non-touch device.

    Of course, all 'compliant' Android phones currently do have touch screens ... so this is effectively moot. But if you want your app to work on non-phone hardware, this might be worth considering.

    There is some good discussion here:

    How to determine if an Android device has a touchscreen?

    https://groups.google.com/forum/?fromgroups=#!topic/android-beginners/cjOVcn0sqLg

提交回复
热议问题