Five implementations of Android binding events for button controls

1、 Write at the beginning

This time, let's introduce the five ways to bind events for the control button in Android.

2、 Specific implementation

First: directly bind to the button control:

Step 1. Set Android: onclick = "" on the button control, where the property value of this property corresponds to the method name in the mainactivity class (self created method):

Step 2. Create the corresponding method in the mainactivity class:

The second way is to use anonymous inner classes:

Step 1. First, get the ID specified in the button control of the layout page in layout:

Step 2. After that, bind the listener for such a button and use the anonymous inner class. The code is as follows:

The third way: using external classes

Step 1. You need to get the ID specified in the button control in the layout page (in mainactivity):

Step 2. Create a class, implement the onclicklistener interface, override the onclick method in this interface, create a context attribute for this method (which needs to be used in the next toast), and use the constructor to set the attribute value:

Step 3. Bind the event for the obtained button and pass in the current object

The fourth way: use mainactivity to directly implement onclicklistener interface

Step 1. Implement onclicklistener interface in mainactivity and override onclick method:

Step 2. Bind the listener corresponding to the button and pass in the current object:

characteristic:

1. In this way, the mainactivity class becomes a listener class, which is very concise

2. However, this is easy to cause structural confusion, because mainactivity class is mainly responsible for initializing the interface, which adds the method of event processor, causing confusion.

3. The interface class needs to implement the method of listener, which is a little out of class.   

The fifth way: use the inner class of members to bind button events

Step 1. Get the ID of the button control in the layout file:

Step 2. Create a member inner class in the mainactivity class, implement the onclicklistener interface, and override the onclick method:

Step 3. Bind the related events in this button, just new internal class (). There is no need to pass in the context object, because this class is the internal class of the current class:

Benefits:

1. Members can access all properties in the external class by means of internal listeners, so it is not necessary to pass in the current object when new onclicklistener implements the class object

2. Member internal listeners can be reused by external classes, because member internal listeners are internal classes of external classes

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>