Android event listening callback mechanism

Source: Internet
Author: User

The Android event listener is an interface of the View class and contains a separate callback method. These methods are called by the android framework when the listener registered in the view is triggered by user interface operations. The callback method is included in the Android event listener interface:

For example, the view object of Android contains a member variable named onclicklistener, and the user's click operation is handed over to the onclick () method of onclicklistener for processing.

To process click events, a developer can define an onclicklistener interface object and assign it to the onclicklistener interface member variable of the view to be clicked. Generally, the setonclicklistener () of the view is used () function to complete this operation. When a user clicks an event, the system calls back The onclick () method of the onclicklistener interface member of the clicked view.

Instance (simulate a button click event listener on the android Interface ):

 
1. define the interface public interface onclicklistener {public void onclick (Button B);} 2. define buttonpublic class button {onclicklistener listener; Public void click () {listener. onclick (this);} public void setonclicklistener (onclicklistener listener) {This. listener = listener;} 3. assign the onclicklistener interface object to the public class activity {public activity () {} public static void main (string [] ARGs) {button = new button (); button. setonclicklistener (New onclicklistener () {@ override public void onclick (Button B) {system. out. println ("clicked") ;}}); button. click (); // user click, system call button. click ();}}

Below are some commonly used callback methods in the Android event listener Interface

Onclick ()

Included in view. onclicklistener. It is called when the user touches this item (in touch mode), or focuses on this item through a browser key or a trackball, And then presses the "OK" key or the trackball.

Onlongclick ()

Included in view. onlongclicklistener. When the user touches and controls the item (in touch mode), or focuses on the item through the Browse key or trackball, then hold the "OK" key or press the trackball (in one second) is called.

Onfocuschange ()

Included in view. onfocuschangelistener. It is called when the user enters or leaves the item by using the Browse key or tracking ball.

Onkey ()

Included in view. onkeylistener. It is called when you focus on this item and press or release a key on the device.

Ontouch ()

Included in view. ontouchlistener. A user-executed action is called as a touch event, including pressing, releasing, or any movement gesture on the screen (within the boundary of this item ).

Oncreatecontextmenu ()

Included in view. oncreatecontextmenulistener. It is called when a context menu is being created (as the result of a continuous "Long click" action ). Refer to the creating menus section of the create menu for more information.

Note:

Among these callback functionsThe onclick () callback does not return a value,However, some other callback methods must return a Boolean value.

Onlongclick ()

True indicates that you have handled this event and so far; false indicates that you have not processed it and/or the event should be handed over to other on-click listeners.

Onkey ()

True indicates that you have handled this event and so far; false indicates that you have not processed it and/or the event should be handed over to other on-key listeners.

Ontouch ()

Returns a Boolean value indicating whether your listener has consumed this event. It is important that this event can have multiple actions that follow each other. Therefore, if you return false when a downward Action event is received, 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 upward Action event.

key events are always submitted to the view with the current focus. They are distributed from the top layer of the view level, and then downward until they reach the appropriate target. If your view (or a subview) has a focus, you can see that the event is distributed by the dispatchkeyevent () method. In addition to intercepting key events from your view, you can also use onkeydown ()
and onkeyup () in your activity to receive all events.

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.