Difference between Click and Touch Listeners in Android

后端 未结 4 550
暗喜
暗喜 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

    0 讨论(0)
  • 2021-02-07 03:58
    • onClickListener is used whenever a click event for any view is raised, say for example: click event for Button, ImageButton.

    • onTouchListener is used whenever you want to implement Touch kind of functionality, say for example if you want to get co-ordinates of screen where you touch exactly.

    Update:

    Just check the official doc for both: onClickListener and onTouchListener.

    So from official doc, definition for both are:

    • onClickListner: Interface definition for a callback to be invoked when a view is clicked.
    • onTouchListener: Interface definition for a callback to be invoked when a touch event is dispatched to this view. The callback will be invoked before the touch event is given to the view.
    0 讨论(0)
  • 2021-02-07 04:01

    The onClickListener is a number of events that are triggered using either the keyboard or the touchscreen. They are performed on a specific view, and the entire view receives the event. In contrast, the onTouchListener is used only for touchscreen events, and they cannot be triggered through the keyboard or any other inputs. They typically also receive the corresponding touch information like the x, y corrdinates, etc.

    I think the onClickListener would be appropriate for your application, if you are not using more complex inputs, like gestures, etc.

    0 讨论(0)
  • 2021-02-07 04:09

    This question I also got in mind that should use click or touch listener.

    Then I have my understandings like this,

    When I need any View(Button/Image/etc) to make clickable that means user just don't touch that part of screen but delebrately try to Touch on that part of screen so the next action gets called I use onClickListener , Also another thing is like suppose working with Button we can make it Clickable True/False as per requirement dynamically,Hence in this situations the OnClickListener is preffered.

    new View.OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                }
            };
    

    While developing screens where the simple Touch of User is to be taken as action like more in Games or Working with Images that you want to capture that where user has touched and also you need to find the Motion Events up/down/left/right of the Touch I preffer to use onTouchListener.

    new View.OnTouchListener() {
    
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    return false;
                }
            };
    

    And In your case I suggest to use the onClickListener

    0 讨论(0)
提交回复
热议问题