Monitor and callback for event handling of Android

Source: Internet
Author: User

The event handling mechanism of Android:

Listener-based event handling
Callback-based event handling

Listening:
Bind specific event listeners for Android components, which are invoked when the corresponding event is triggered;

Three objects:
1. The event source is the place where the event occurs, usually the individual components
2. Events
3. Event Listener

Four ways to listen to events:

When the control calls Setonclicklistener (), it needs to pass in a onclicklistener parameter. By looking at the source code, we will find that Onclicklistener is an interface.
So, we can consider three ways to implement the functionality of this interface:
1. Create an external class that implements the interface:
Cases:

publicclass ClickOkListener implements OnClickListener{    //重写OnClick方法}//**************************************////在Activity中调用btn.setOnClickListener(new ClickOkListener)

2. How anonymous internal classes are
Cases:

btn.setOnClickListenerOnClickListener(){    onClick()    {        //处理事件    }});

3.Activity as Event listener
(1). Btn.setonclicklistener (This)
(2). Let activity implements Onclicklistener
(3). Overriding the OnClick method in activity

In addition, Android allows developers to bind the handling of a point-and-click event by setting the OnClick property in XML.

Callback:
Handles Android component-specific callback methods.
In the callback's event-handling model, the event source and event listener are unified, and when the user triggers the event, the component's own specific method handles the event.

Common callback methods in the view class:
The OnKeyDown button is pressed
Onkeylongpress button is long pressed
OnKeyUp Button is released
Ontouchevent Triggering touch screen events

Action steps for callback events:
1. Custom control Classes
2. Overriding the callback method of the parent class in the custom control
3. In the XML layout file, use the custom control directly

Add: Almost every event-handling method has a Boolean-type return value that indicates whether the processing method fully handles the event.
If true, the event is handled and the event is not passed.
If False is returned, the event is not handled and the event is passed down.

Sequence of event handling:
1. Monitoring
2. Callbacks
3. The current Activity

Copyright notice: Just out of the original content of the pot, I hope you have help ~

Monitor and callback for event handling of Android

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.