First recognized Android callback mechanism, first recognized android callback

Source: Internet
Author: User

First recognized Android callback mechanism, first recognized android callback

The interface callback mechanism is visible everywhere in Android, especially in UI event processing. The most common example is the button click event. The button has an onClick () method. We know that onclick () is a callback method. When you click a button, this method is executed.

In Java, callback is implemented through interfaces and internal classes:
1. Define the OnItemClickClass interface, including the callback method OnItemClick ()
2. Declare An OnItemClickClass interface object onItemClickClass in a class CallerClass.
3. Assign the interface member (onItemClickClass) of the Caller object to an internal class object in the program, as shown in figure
New OnItemClickClass (){
OnItemClick (){
// Function implementation
}
In this way, the onItemClickClass interface member of the CallerClass object can call the OnItemClick () method as needed to complete the callback.

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 the Button
public 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 interface member of the Button.
public class Activity {  public Activity() {  }  public static void main(String[] args) {    Button 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();  }}




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.