On the callback method of Android event listener

Source: Internet
Author: User

Http://developer.51cto.com/art/201001/180846.htm

      The Android Event listener acts as an interface to the view class, which contains a number of callback methods, such as: OnClick (); Onlongclick (); Onfocuschange (); OnKey (); OnTouch () ; Oncreatecontextmenu () and so on.

In the Android operating system, the handling of events is a very basic and important operation. The implementation of many functions requires triggering of related events to achieve their goals. For example, the Android event listener is the interface of the view class, and contains a separate callback method. These methods are called by the Android framework when a listener registered in the view is triggered by a user interface action. The following callback methods are included in the Android Event listener interface:

OnClick ()

Included in View.onclicklistener. Called when the user touches the item (in touch mode), or is focused on the item with the browse key or the track ball, and then presses the "confirm" key or presses the tracking ball.

Onlongclick ()

Included in View.onlongclicklistener. Called when the user touches and controls the item (in touch mode), or is focused on the item with the browse key or the track ball, and then keeps pressing the "confirm" key or pressing the track ball (one second).

Onfocuschange ()

Included in View.onfocuschangelistener. Called when the user enters or leaves the item using the Browse key or the track ball.

OnKey ()

Included in View.onkeylistener. Called when the user focuses on the item and presses or releases a key on the device.

OnTouch ()

Included in View.ontouchlistener. When a user performs an action that is invoked as a touch event, including a press, release, or any mobile gesture on the screen (within the bounds of this item).

Oncreatecontextmenu ()

Included in View.oncreatecontextmenulistener. Called when a context menu is being created (as a result of a persistent "long click" Action). See the Creating Menu Creating Menus section for more information.

These methods are the only "tenants" of their corresponding interfaces. To define these methods and handle your events, implement this nested interface in your activity or define it as an anonymous class. Then, pass an instance of your implementation to the respective View.set ... Listener () method. (for example, call Setonclicklistener () and pass it on to your onclicklistener implementation. )

The following example shows how to register a click Listener for a button:

  1. Create an anonymous implementation of Onclicklistener
  2. Private Onclicklistener Mcorkylistener  =  New Onclicklistener () {
  3. public void OnClick (View v) {
  4. Do something when the button is clicked
  5. }
  6. };
  7. protected void OnCreate (Bundle savedvalues) {
  8. ...
  9. Capture our buttons from layout
  10. Button Button  = (Button) Findviewbyid (R.id.corky);
  11. Register the OnClick listener with the implementation above
  12. Button.setonclicklistener (Mcorkylistener);
  13. ...
  14. }

You may find it much easier to make onclicklistener as part of the activity. This avoids additional class loading and object assignment. Like what:

    1. public class exampleactivity extends activity  implements onclicklistener { 
    2. protected void oncreate ( bundle savedvalues)  { 
    3. ...  
    4. button& nbsp; button  =  (Button) Findviewbyid (r.id.corky);  
    5. Button.setonclicklistener (this);  
    6. }  
    7. // implement the onclicklistener callback  
    8. Public void onclick (view v)  { 
    9. // do  something when the button is clicked  
    10. }  
    11. ...  

Note that the onclick () callback in the above example does not return a value, but some other Android event listeners must return a Boolean value. Cause and event related. For some of these, the reasons are as follows:

· Onlongclick () – Returns a Boolean value indicating whether you have consumed the event and should not process it further. That is, returning true means that you have handled the event and that this is the end; Returning false indicates that you have not handled it and/or that the event should continue to be handed to other On-click listeners.

· OnKey () – Returns a Boolean value indicating whether you have consumed the event and should not process it further. That is, returning true means that you have handled the event and that this is the end; Returning false indicates that you have not handled it and/or that the event should continue to be handed to other on-key listeners.

· OnTouch ()-Returns a Boolean value that indicates whether your listener has consumed this event. The important thing is that this event can have multiple actions that follow each other. Therefore, if you return False when you receive a down action event, it indicates that you have not consumed the event and are not interested in subsequent actions. Then, you will not be called by other actions in the event, such as gestures or the last occurrence of an upward action event.

Remember that key events are always submitted to the view where the current focus is. They are distributed from the top of the view hierarchy and then down until they reach the appropriate destination. If your view (or a child view) currently has focus, you can see that the event is distributed through the Dispatchkeyevent () method. In addition to intercepting key events from your view, there is an alternative that you can use OnKeyDown () and onKeyUp () in your activity to receive all events.

Note: Android will first invoke the event handler, followed by the appropriate default processor in the class definition. In this way, returning true from these things listeners propagates the stop event to other Android event listeners and also blocks the callback function for the missing event handler in the view. So when you return true, confirm that you want to terminate the event.

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.