[Android] zero basis to soaring | event handling mechanism based on listening

3.1.1 event handling mechanism based on listening

classification Android basics tutorial

Introduction to this section:

In Chapter 2, we learn about Android UI controls. We can use these controls to form a beautiful interface, but it's just an interface; The next step is to start learning logic and business implementation. This chapter explains the event processing mechanism of Android! What is the event handling mechanism? Take a simple example. For example, click a button and we send a login request to the server! Of course, there is more than one event handling mechanism in Android. For example, when the screen is selected, we click on an area on the screen Simply put, the event handling mechanism is that when we interact with the UI, we just add some small actions behind it! 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 listening:

Process model diagram:

Written expression:

The event listening mechanism consists of three types of objects: event source, event and event listener. The processing flow is as follows: Step 1: set a listener for an event source (component) to listen to user operations Step 2: user operations, The listener that triggered the event source Step 3: generated the corresponding event object Step 4: passed the event source object as a parameter to the event listener step 5: the event listener judged the event object and executed the corresponding event processor (the processing method of the corresponding event)

induce:

Event listening mechanism is a delegated event processing mechanism. The event processing of the event source (component) is delegated to the event listener. When a specified event occurs in the event source, it will notify the specified event listener to perform corresponding operations

2. Five different forms of use:

We use the following: a simple button click to prompt Toast information; Use five different forms to achieve!

design sketch:

1) Use anonymous inner classes directly

The most commonly used method: directly setxlistener, and then rewrite the method inside; It is usually used temporarily, and the reusability is not high!

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;

</span><span class="lit">@Override</span><span class="pln">    
</span><span class="kwd">protected</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onCreate</span><span class="pun">(</span><span class="typ">Bundle</span><span class="pln"> savedInstanceState</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">    
    </span><span class="kwd">super</span><span class="pun">.</span><span class="pln">onCreate</span><span class="pun">(</span><span class="pln">savedInstanceState</span><span class="pun">);</span><span class="pln">    
    setContentView</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">layout</span><span class="pun">.</span><span class="pln">activity_main</span><span class="pun">);</span><span class="pln">    
    btnshow </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">Button</span><span class="pun">)</span><span class="pln"> findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">btnshow</span><span class="pun">);</span><span class="pln">    
    btnshow</span><span class="pun">.</span><span class="pln">setOnClickListener</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">OnClickListener</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">    
        </span><span class="com">//Override the handling method of click event onclick() < / span > < span class = "PLN" >
        </span><span class="lit">@Override</span><span class="pln">    
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onClick</span><span class="pun">(</span><span class="typ">View</span><span class="pln"> v</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">    
            </span><span class="com">//Display Toast information < / span > < span class = "PLN" >
            </span><span class="typ">Toast</span><span class="pun">.</span><span class="pln">makeText</span><span class="pun">(</span><span class="pln">getApplicationContext</span><span class="pun">(),</span><span class="pln"> </span><span class="str">"You clicked the button"</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Toast</span><span class="pun">.</span><span class="pln">LENGTH_SHORT</span><span class="pun">).</span><span class="pln">show</span><span class="pun">();</span><span class="pln">    
        </span><span class="pun">}</span><span class="pln">    
    </span><span class="pun">});</span><span class="pln">    
</span><span class="pun">}</span><span class="pln">        

}

2) Use inner classes

Different from the anonymous inner class above! Advantages: it can be reused in this class and can directly access all interface components of the external class!

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 inner class object as a parameter    
        btnshow.setOnClickListener(new BtnClickListener());    
    }     
    //Define an internal class to implement view Onclicklistener interface and override the onClick() method    
    class BtnClickListener implements View.OnClickListener    
    {    
        @Override    
        public void onClick(View v) {    
            Toast.makeText(getApplicationContext(), "The button was clicked", Toast.LENGTH_SHORT).show();   
        }    
    }    
} 

3) Use external classes:

Is to create another Java file to process events. This form is less used! Because the external class cannot directly access the components in the user interface class, the components should be passed in for use through the construction method; The result is that the code is not concise enough!

ps: to demonstrate parameter passing, TextView is used instead of Toast hint!

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 implements OnClickListener {
private TextView textshow;
//Pass the text box as a parameter
public MyClick(TextView txt)
{
textshow = txt;
}
@Override
public void onClick(View v) {
//Click to set the text displayed in the text box
textshow.setText("click the button!");
}
}

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);
//Directly new an external class and pass TextView as a parameter
btnshow.setOnClickListener(new MyClick(txtshow));
}
}

4) Use Activity directly as an event listener

Just let the Activity class implement the XxxListener event listening interface, define and rewrite the corresponding event handler method in the Activity. Eg: the Activity implements the OnClickListener interface and overrides the onClick(view) method. When adding the event listening object for some components, 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 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">Button</span><span class="pun">)</span><span class="pln"> findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">btnshow</span><span class="pun">);</span><span class="pln">    
    </span><span class="com">//Write this < / span > < span class = "PLN" >
    btnshow</span><span class="pun">.</span><span class="pln">setOnClickListener</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">);</span><span class="pln">    
</span><span class="pun">}</span><span class="pln">    
</span><span class="com">//Rewrite the abstract method in the interface < / span > < span class = "PLN" >
</span><span class="lit">@Override</span><span class="pln">    
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onClick</span><span class="pun">(</span><span class="typ">View</span><span class="pln"> v</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">    
    </span><span class="typ">Toast</span><span class="pun">.</span><span class="pln">makeText</span><span class="pun">(</span><span class="pln">getApplicationContext</span><span class="pun">(),</span><span class="pln"> </span><span class="str">"Click the button"</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Toast</span><span class="pun">.</span><span class="pln">LENGTH_SHORT</span><span class="pun">).</span><span class="pln">show</span><span class="pun">();</span><span class="pln">         
</span><span class="pun">}</span><span class="pln">         

}

5) Bind directly to label:

Define an event handling method eg:public void myClick(View source) source corresponding to the event source (component) directly in the corresponding Activity in the xml layout file, then set an attribute corresponding to the composition of the event to be triggered in the layout file: 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);
}
//Customize a method and pass in a view component as a parameter
public void myclick(View source)
{
Toast.makeText(getApplicationContext(), "button 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"/>    
 </LinearLayout> 
 

Summary of this section

This section introduces the event handling mechanism in Android. The onClickListener click event in the example. Of course, there are other events besides this, such as onItemClickListener. All events that need to pass setxxlistener are basically based on event listening! In addition, most of the five methods are: 1, 2, 3 and 5, depending on the specific situation~

Keywords: Java Android Design Pattern interface

Added by chrisdburns on Sat, 22 Jan 2022 20:11:27 +0200