Android button click events and how listeners are implemented
Source: Internet
Author: User
<span id="Label3"></p><p><span style="font-family: Microsoft YaHei;">the <span style="font-family: Microsoft YaHei;">first Type: anonymous inner class as event listener class</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;"></span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">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, which is, in fact, the most extensive form of event listener Currently. The above program code is an anonymous inner class to create event listeners!!! </span></span><br><span style="font-family: Microsoft YaHei;">the <span style="font-family: Microsoft YaHei;">only drawback to using anonymous inner classes as listeners is that the syntax of the anonymous inner class is a bit difficult to master, and if the Reader's Java Foundation is solid and the syntax of the anonymous inner class is better, it is generally advisable to use an anonymous inner class as the Listener. </span></span><br><span style="font-family: Microsoft YaHei;">the <span style="font-family: Microsoft YaHei;">second type: inner class as listener</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;"></span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">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!! </span></span><br><span style="font-family: Microsoft YaHei;">the <span style="font-family: Microsoft YaHei;">third type: activity itself as an event listener</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">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. </span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">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. </span></span><br><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">fourth type: external class as Listener</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">Buttontest class</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;"></span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">The Mybuttonlistener listener is triggered by the program when the user clicks the button</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">external Mybuttonlistener class</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;"></span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">It is uncommon to define event listener classes using top-level classes, mainly because of the following two reasons:</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">1, Event Listener is usually a specific GUI interface, defined as external class does not improve the cohesion of the Program. </span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">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. </span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">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. </span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">fifth: bind directly to the label</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">Android also has a simpler way of binding event listeners to bind event handlers directly in the interface layout file for the specified tag. </span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">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</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">(View Source)</span></span><br><span style="font-family: Microsoft YaHei;">method <span style="font-family: Microsoft YaHei;">name of the Method. To add a property to a button in a layout file, such as Code:</span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;"></span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">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. </span></span><br><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">Here is the Java code for the interface layout:</span></span></p><p><p></p></p>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service