Ontouch and onclick and Onlongclick events for Android view

Source: Internet
Author: User

The sequence of calls for these three events is:

Ontouch->onlongclick->onclick

Let's look at the handling functions of these three events:

    1. public boolean OnTouch (View V, motionevent event);
    2. public boolean Onlongclick (View v);
    3. public void OnClick (View v);

See the return values of three functions, only the onclick is void, and Ontouch and Onlongclick are Boolean, because the system handles these events conditionally and must satisfy the condition to trigger the corresponding event handler.

Events are passed through the chain of events, and if the level is not processed, it is passed down to the next level until the entire chain of events is propagated.

Ontouch returns true to indicate that the event has been processed, that the event is not continued to be passed in the event chain, and that if the Onlongclick returns true, the table name handles the event and does not continue to pass the event in the event chain.

If you want the OnClick event in the view to coexist with the Ontouch event, and the two events are triggered individually on a single button, you can simply remove the onclick event and subdivide it directly in the Ontouch event, if the position is pressed and the position is lifted, i.e. startx== LASTX, the OnClick event is considered to be the following code:

@Override Public BooleanOnTouch (View V, motionevent event) {Switch(Event.getaction ()) { Casemotionevent.action_down:{//record the x-coordinate of the pressed pointStartX =event.getrawx ();  Break; }         Casemotionevent.action_up://record the x-coordinate of the raised point, if two times equal, trigger the Click event, otherwise it is a sliding event            floatLASTX =event.getrawx (); if(StartX = =lastx) {Toast.maketext (Getapplication (),"Clicked", Toast.length_short). Show (); }Else{Toast.maketext (Getapplication (),"Slide.", Toast.length_short). Show (); }                          Break;  Casemotionevent.action_move:{ Break; }         }        return false; }

Ontouch and onclick and Onlongclick events for Android view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.