Add a response event to the button (the buttons), and you need to set the Listener (Listener) for it. This article summarizes several button Listener that are commonly used in Android.
The first type: Anonymous inner class as event listener class
Button button = (button) Findviewbyid (R.id.button);//The button object Button.setonclicklistener is instantiated through a resource with a resource internal ID of button (new Onclicklistener {//binds the anonymous listener and executes the logic code that you want to execute after clicking the button public void OnClick (View v) {toast.maketext (Myactivity.this, "clicked Button", Toast.length_long). Show ();}});
Most of the time, the event handler has little leverage (the available code is often abstracted as a business logic approach), so most event listeners are used only once, so it is more appropriate to use an anonymous inner class as an event listener, and for the use of anonymous inner classes as listeners, The only drawback is that the syntax of the anonymous inner class is a little tricky to master.
The second type: inner class as listener
public class Buttonlistener extends Activity {public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinst Ancestate); Setcontentview (R.layout.main); Button button = (button) Findviewbyid (R.id.button); MyListener MyListener = new MyListener (); Button.setonclicklistener (MyListener); } class MyListener implements Onclicklistener {public void OnClick (View v) {System.out.println ("Create button listener using inner class"); } }}
Defines the event listener class as the inner class of the current class. 1. Use an inner class to reuse the listener class in the current class, because the listener class is the inner class of the outer class and 2, so you have free access to all interface components of the external class. This is also the two advantages of the inner class. The above code is the form of an inner class!!
The third type: Activity itself as an event listener
<ignore_js_op> This form uses activity itself as the listener class, and the event handler method can be defined directly in the activity class, which is very concise. However, there are two drawbacks to this approach: (1) This form may cause confusion in the structure of the program. The main function of the activity should be to complete the interface initialization, but also include the event handler method at this time, causing confusion. (2) If the activity interface class needs to implement the listener interface, it makes people feel more strange.
The program above allows the activity class to implement the Onclicklistener event listener interface, allowing the event handler method to be defined directly in the Activity class: OnClick (View v), when the event listener object is added for a component, Use this directly as an event listener object.
Fourth type: External class as Listener
Buttontest class
<ignore_js_op>
The Mybuttonlistener listener is triggered by the program when the user clicks the button
External Mybuttonlistener class
<ignore_js_op>
It is uncommon to define event listener classes using top-level classes, mainly because of the following two reasons:
1, event listener is usually a specific GUI interface, defined as external class does not improve the cohesion of the program.
2. The external class event listener does not have free access to the components in the class that created the GUI interface, and the programming is not concise.
However, if an event listener does need to be shared by multiple GUI interfaces and is primarily an implementation of some kind of business logic, consider defining an event listener class in the form of an external class.
Fifth: Bind directly to the label
Android also has a simpler way of binding event listeners to bind event handlers directly in the interface layout file for the specified tag.
For many Android tags, they support attributes such as onclick, Onlongclick, and the property value of a property that is a shape such as xxx
(View Source)
Method name of the method. To add a property to a button in a layout file, such as code:
<ignore_js_op>
Bind an event-handling method to the button buttons: Clickhanlder, which means that the developer needs to define a void Clickhanler (View source) method in the corresponding activity in the interface layout. This method will be responsible for handling the Click event on the button.
Here is the Java code for the interface layout:
<ignore_js_op>
How to implement the common button listener for Android