Listening-based event processing mechanism


Introduction to this section:

In Chapter 2, we are learning Android UI controls. We can use these controls to form a beautiful interface, but it is just an interface; next step It’s time to start learning logic and business implementation. This chapter explains Android’s event processing mechanism! What is event handling mechanism? Give an example A simple example, such as clicking a button, we send a login request to the server! Of course, there is more than just this kind of event processing mechanism in Android. For example, when a selection occurs on the screen, we click on a certain area on the screen... To put it simply, the event processing mechanism is that when we interact with the UI, we just add some small actions behind the scenes! In this section we will introduce the most frequently used one: the event processing mechanism based on listening!


1. Time processing mechanism model based on monitoring:

Process model diagram:

1.jpg

Text description:

The event listening mechanism consists of event source,event,Event listenerComposed of three types of objects The processing flow is as follows: Step 1:Set a listener for an event source (component) to monitor user operationsStep 2:The user's operation triggers the monitoring of the event source Step 3:The corresponding event object is generatedStep 4:Pass this event source object as a parameter to the event listenerstep 5:The event listener pair The event object is judged and the corresponding event processor (corresponding event processing method) is executed

Summary:

The event listening mechanism is a Delegated event processing mechanism, event source (component) event processing is delegated to event listeners When the specified event occurs in the event source, the specified event listener will be notified and the corresponding operation will be performed


2. Five different usage forms:

We use the following : A program that prompts Toast information by simply clicking a button; uses five different forms to achieve it!

Rendering:

2.png


1) Directly use anonymous inner classes

The most commonly used one: directly setXxxListener and then rewrite the method inside; It is usually used once temporarily and has low reusability!

The implementation code is as follows: MainActivity.java:

package com.jay.example.innerlisten;    
    
import android.os.Bundle;    
import android.view.View;    
import android.view.View.OnClickListener;    
import android.widget.Button;    
import android.widget.Toast;    
import android.app.Activity;    
    
    
public class MainActivity extends Activity {    
    private Button btnshow;    
        
    @Override    
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_main);    
        btnshow = (Button) findViewById(R.id.btnshow);    
        btnshow.setOnClickListener(new OnClickListener() {    
            //重写点击事件的处理方法onClick()    
            @Override    
            public void onClick(View v) {    
                //显示Toast信息    
                Toast.makeText(getApplicationContext(), "你点击了按钮", Toast.LENGTH_SHORT).show();    
            }    
        });    
    }        
}

2) Using inner classes

is different from the anonymous inner class above! Advantages of use: It can be reused in this class and can directly access all interface components of external classes!

The implementation code is as follows: MainActivity.java:

package com.jay.example.innerlisten;

import android. os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {
private Button btnshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
btnshow = (Button) findViewById(R.id.btnshow);
//Directly new an internal class object as a parameter
btnshow .setOnClickListener(new BtnClickListener());
                                                                                                                                                         
                                                                                                                                                                                                                                                                           
}
}


3) Use external classes:

is to create another Java file to handle events. This form is rarely used! Because external classes cannot directly access the user interface The components in the class must be passed in through the construction method; the result of this is that the code is not concise enough!

ps: In order to demonstrate parameter passing, TextView is used here instead of Toast prompt!

3.png

The implementation code is as follows: MyClick.java:

##package com.jay.example.innerlisten;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MyClick implementations OnClickListener {
private TextView textshow ;
//Pass the text box as a parameter
public MyClick(TextView txt)
{
textshow = txt;
}
@Override
public vo id onClick(View v) {
//Set the text displayed in the text box after clicking it
textshow.setText("The button was clicked!");
}
}

MainActivity.java

package com.jay.example.innerlisten;    
import android.os.Bundle;    
import android.widget.Button;    
import android.widget.TextView;    
import android.app.Activity;    
    
    
public class MainActivity extends Activity {    
    private Button btnshow;    
    private TextView txtshow;    
    @Override    
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_main);    
        btnshow = (Button) findViewById(R.id.btnshow);    
        txtshow = (TextView) findViewById(R.id.textshow);    
        //直接new一个外部类,并把TextView作为参数传入    
        btnshow.setOnClickListener(new MyClick(txtshow));    
    }         
}

4) Use Activity directly as an event listener

You only need to let the Activity class implement the XxxListener event listening interface, and define and override the corresponding event handler method in the Activity eg: Activity implements the OnClickListener interface, overrides the onClick(view) method and adds the event listening object for certain components. When, you can directly setXxx.Listener(this)

The implementation code is as follows: MainActivity.java:

##package com.jay.example. innerlisten;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android. widget.Toast;
import android.app.Activity;

//Let the Activity method implement the OnClickListener interface
public class MainActivity extends Activity implements OnClickListener{
private Button btnshow;
@ Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnshow = (Button) findViewById(R.id .btnshow);
//Write this directly
btnshow.setOnClickListener(this);
}
//Rewrite the abstract method in the interface
@Override
public void on Click (View v) {      
          Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_SHORT).show();        
                                 

5) Bind directly to the tag:

is to define an event processing method directly in the corresponding Activity in the xml layout file eg:public void myClick(View source) source corresponds to the event source (component) Then in the layout file corresponding to the component to trigger the event, set an attribute: onclick = "myclick"

The implementation code is as follows: MainActivity.java:

package com.jay.example.caller;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R .layout.activity_main);       
                                                                                                                                                                                                                         Layout. ), "The button was clicked", Toast.LENGTH_SHORT).show();
}
}

main.xml layout file:

##<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height=" match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: text="button"
                                                          android:onClick="myclick"/> The event processing mechanism in Android is used in the example. The onClickListener click event is used in the example. Of course, there are other events besides this, such as onItemClickListener. Everything that requires setXxxListener is basically based on event monitoring! In addition, these five methods are more commonly used: 1, 2, 3, and 5, depending on the specific situation~