First, write at the front
This time, let's introduce the five ways that Andong--button bind events for controls.
Second, the concrete realization
First: Bind directly to the button control:
Step 1, set android:onclick= on the button control, where the property value for this property corresponds to the method name in the Mainactivity class (the method that you created):
Step 2 to create a corresponding method in the Mainactivity class:
public void Demo (view view) {
Toast.maketext (mainactivity.this, "second button clicked", Toast.length_short). Show ();
Second: Using anonymous inner classes:
Step 1: First you need to obtain the ID specified in the button control of the layout page in layout.
Step 2: After that, for this button to bind the listener, use the anonymous inner class in the way that the code is as follows.
Button = (button) Findviewbyid (r.id.button1);
Button.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (view view) {
Toast.maketext (Mainactivity.this, "through anonymous inner class: The first button was clicked", Toast.length_short). Show ();
Third: the way to use external classes
Step 1: You need to obtain the ID (in mainactivity) that is specified in the button control on the Layout layout page.
button= (Button) Findviewbyid (R.id.button2)
Step 2: Create a class, implement the Onclicklistener interface, override the OnClick method in this interface, and create a context property for this method (after which the toast needs to be used), and use the constructor to set this property value.
Package com.mqz.android_event_test;
Import Android.content.Context;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Toast;
public class Btntest implements Onclicklistener {
private context context;
Public Btntest {
This.context=context
}
@Override public
void OnClick (view view) {
Toast.maketext, "implements the Onclicklistener interface through an external class: The first button was clicked", Toast.length_short). Show ();
}
Step 3: Bind the event for the button you get, and pass the current object into the
Button.setonclicklistener (New btntest (This))
Fourth: How to implement the Onclicklistener interface directly using mainactivity
Step 1 Implement the Onclicklistener interface in Mainactivity and override the OnClick method:
Step 2: The Binding button buttons correspond to the listener, passing the current object to the
Characteristics:
1. This is the Mainactivity class becomes the listener class, which is very concise
2. But this can easily cause confusion in the structure, because the main responsibility of the Mainactivity class is to initialize the interface, which adds to the event handler method, causing confusion.
3. The interface class needs to implement the listener method, a little nondescript.
Package com.mqz.android_event_test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Mainactivity extends activity implements onclicklistener{
Private button button;
@Override public
void OnClick (view view) {
Toast.maketext (mainactivity.this, " Implement the Onclicklistener interface via mainactivity: The first button was clicked, Toast.length_short. Show ();
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Button = (button) Findviewbyid (r.id.button2);
Button.setonclicklistener (this);
}
Fifth: Implement the binding of the Button button event using the member inner class
Step 1. Gets the Id of the button control in the layout layout file:
button= (Button) Findviewbyid (R.id.button2)
Step 2 Create a member inner class in the Mainactivity class and implement the Onclicklistener interface to override the OnClick method:
Class BtnTest1 implements onclicklistener{
@Override public
void OnClick (view view) {
Toast.maketext ( Mainactivity.this, "through member Inner class: Second button was clicked", Toast.length_short). Show ();
Step 3, bind related events in this button, the new inner class (), you do not need to pass in the context object, because this class is the inner class of the current class:
Benefits:
1. A member internal listener can access all properties in the external class, so you do not need to pass in the current object when new Onclicklistener implements the class object
2. member internal listeners can be reused by external classes because the listener inside the member is the inner class of the outer class
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.