Android learning notes: Event Processing Mechanism

Source: Internet
Author: User

Android provides a powerful event processing mechanism, including two sets of event processing mechanisms:

  1. Listener-based event processing
  2. Callback-based event processing

For listener-based event processing, the main approach is to bind a specific event listener to the android interface component. In addition, Android allows you to use the Android: onclick attribute in the interface layout file to specify the event listening method, the developer needs to define the event listening method in the activity (this method must have a view-type parameter to represent the clicked UI component). When you click this UI component, the system will inspire the method specified by the Android: onclick attribute.

For callback-based event processing, the main method is to rewrite the specific callback method of the android component or the callback method of the activity. Generally, the code of callback-based event processing method is concise and can be used to process some universal events.

The event processing Listening model mainly involves the following three types of objects:

  1. Event Source: the location where an event occurs, usually the components
  2. Event: Events encapsulate the specific events that occur on the interface components. Generally, they are obtained through the event object.
  3. Event listener: monitors events in the event source and responds accordingly.

For the callback-based event processing model, the event source and event listener are unified, or the event listener disappears completely. When a user fires an event on a GUI component, the component's own method is responsible for processing the component.

Almost all callback-based event processing methods have a boolean type return value.If the returned value is true, it indicates that the processing method has completely handled the event and the event will not be propagated. If the returned value is false, it indicates that the processing method has not completely handled the event, this event will be propagated.

Note: The Android system first triggers the event listener bound to the key, then triggers the Event Callback method provided by the component, and then transmits the event to the activity of the component.

We can see that the listener-based event processing model has a greater advantage:

  1. The listener-based event processing model has a clearer division of labor. The event source and event listening are implemented by two classes respectively, with better maintainability.
  2. Android event processing mechanism ensures that listener-based event listeners are triggered preferentially

However, in some cases, the callback-based event processing mechanism improves the cohesion of the program. Take the previousSmall Ball Moving with fingersFor example, this form improves the cohesion of the program. You do not need to bind an event listener to the view in the activity class, because the view can handle touch screen events by itself. In general,Callback-based event processing is more suitable for viewing with fixed event processing logic.

01. public class drawview extends view 02. {03. private float currentx = 40; 04. private float currenty = 50; 05. // define and create the paint brush 06. paint P = new paint (); 07. public drawview (context) 08. {09. super (context); 10 .} 11. 12. public drawview (context, attributeset set) 13. {14. super (context, set); 15 .} 16. 17. @ override 18. protected void ondraw (canvas) 19. {20. // set the paint brush color 21. p. setcolor (color. red); 22. // draw the circle 23. canvas. drawcircle (currentx, currenty, 15, p); 24 .} 25. 26. @ override 27. public Boolean ontouchevent (motionevent event) 28. {29. // obtain (update) Coordinates 30. this. currentx = event. getx (); 31. this. currenty = event. gety (); 32. // notify the current component to redraw 33. this. invalidate (); 34. return true; 35 .} 36 .}

 

Related Article

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.