Android Development event response-listener-based event response, android listener
Android Development event response-listener-based Event Response
This article describes how the Android operating system responds to events through listening.
- Android Development event response-listener-based Event Response
-
- Background
- Android Development event response type
- Internal class
- Anonymous internal class
- External class
- Bind tags directly
- Summary
Background
For any visual development, the response to the control is involved. For example, we can click the Button to explain how to deal with events in Android.
Android Development event response type
In Android development, there are two ways to respond to events:
-Listener-based event processing
-Callback-based event processing
In this article, we focus on analyzing listener-based event processing in five ways:
-Internal class
-Internal Anonymous class
-External class
-Bind tags directly
-The Activity itself serves as the event listening tag
I personally have no sense of the fifth method, so I will not detail it here.
Internal class
The internal class is our most commonly used method. First, we need to use setOnClickListener to bind the class for processing response messages:
// Internal class button2.setOnClickListener (new MyOnClickListener ());
Then, create an internal class in the Activity you designed:
Public class MyOnClickListener implements View. onClickListener {@ Override public void onClick (View v) {Toast. makeText (getApplicationContext (), "internal class", Toast. LENGTH_SHORT ). show ();}}
I personally think that the internal class is the first method, because you can design an internal class to accept all the response messages from the button.
Anonymous internal class
The following is an example of an anonymous internal class:
// Internal Anonymous class button1.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Toast. makeText (getApplicationContext (), "Internal Anonymous class", Toast. LENGTH_SHORT ). show ();}});
External class
In general, the external class is to create a. java file to compile the response to the Button click event. The following is an example. OuterClass is the class that I wrote to respond to the Button event:
Import android. app. activity; import android. view. view; import android. widget. toast;/*** Created by zhi on 2015/4/24. */public class OuterClass implements View. onClickListener {private Activity act; OuterClass (Activity activity) {act = activity ;}@ Override public void onClick (View v) {Toast. makeText (act, "external class", Toast. LENGTH_SHORT ). show ();}}
Because Toast requires an Activity parameter, you need to set this variable when constructing this class.
During the call, the parameters are passed over:
// External class button3.setOnClickListener (new OuterClass (this ));
Bind tags directly
Directly bind a tag, which is set in an xml file, as shown in the following example:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button4" android:id="@+id/button4" android:layout_below="@+id/button3" android:layout_alignStart="@+id/button3" android:onClick="clickButton"/>
In the layout file, there is an onClick label to define the function bound to this button.
Tip:Here, "clickButton" is the function name for processing the message, and no brackets are required.
In the. java file, compile the clickButton method:
// Directly bind public void clickButton (View source) {Toast. makeText (getApplicationContext (), "bind tag", Toast. LENGTH_SHORT). show ();}
Tip:Note that the function name can be named at will,ParametersBe sure to comply with the (View source) form, or the program will crash directly.
Summary
This topic describes how to respond to events in the Android operating system. This topic describes how to use listeners.