Android Event handling

Source: Internet
Author: User

Android provides two kinds of event handling mechanisms :

    • Listener-based event handling (delegated event handling)
    • Callback-based event handling

When an event source is triggered (event activity occurs), the first thing the Android system triggers is an event listener bound to the event source, which, if propagated, then triggers the event callback method provided by the event source.

The processing model for the Android Listener-based event processing is as follows:

Event sources, event listeners, registering event listeners

The event source can be any interface component, and registering an event listener is the Setxxxlistener (Xxxlistener) method that invokes the event source, and the key is to implement the event listener class. View.onclicklistener is the interface that the touchscreen event listener must implement, and the event handler method in the event handler is called by the system.

Android Callback-based event handling :

For Android callback-based event handling, the main approach is to rewrite the Android component-specific callback method, or rewrite the activity's callback method. Android provides callback methods for the event response for most of the interface components, as long as you rewrite them. In general, callback-based event processing can be used to handle some common events, and callback-based event-handling code is more concise, but for certain event handling, callback-based event handling is not available, only listener-based event handling.

Almost all callback-based event-handling methods have a Boolean-type return value that identifies whether the processing method can handle the event fully, is true, indicates that it can be processed, and false indicates that it cannot be processed and continues to propagate.

The following is a touchscreen-based callback processing method:

public boolean ontouchevent (Motionevent event): Corresponding touch screen events
Parameter event: The event is an object of the mobile screen touch events wrapper class, which encapsulates all information about the event, such as the location of the touch, the type of touch, and the time of the touch, which is created when the user touches the phone.
Return value: The return value of the method is return true when the listener has completely handled the change event and does not want other callback methods to process again to return false otherwise.
The type of event that the user touches is obtained through event.getaction, with the touch event type:
Motionevent.action_down//Screen is pressed
MOTIONEVENT.ACTION_UP//Screen pressed to be lifted
Motionevent.action_move//drag in the screen
Get the x, y coordinates of the touch position (convertible to other types, such as int) Event.getx () by Event.getx () and Event.gety () (return value float type)

Follow-up for further information: http://www.cnblogs.com/xiaoweiz/p/3803301.html
http://wangkuiwu.github.io/2015/01/03/TouchEvent-View/

Android Event handling

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.