5 implementation methods of clicking events and listeners for Android buttons

Source: Internet
Author: User

5 implementation methods of clicking events and listeners for Android buttons

 

Reprinted from: http://hi.baidu.com/zealot886/item/4ddc9a2abdff98c2ddf69a46

 

 

First: anonymous internal class as event listener class

Most of the time, the event processor has no exploitation value (the code that can be exploited is usually abstracted into the business logic method). Therefore, most event listeners are only used temporarily, therefore, it is more appropriate to use event listeners in the form of anonymous internal classes. In fact, this form is currently the most extensive form of event listeners. The above program code is an anonymous internal class to create an event listener !!!

The only drawback of using an anonymous internal class as the listener is that the syntax of the anonymous internal class is a little difficult to grasp. If the reader has a solid Java Foundation, the syntax of the anonymous internal class is better, we recommend that you use an anonymous internal class as the listener.

Type 2: internal class as listener

Define the event listener class as the internal class of the current class. 1. You can use the internal class to reuse the listener class in the current class, because the listener class is an internal class of the external class. 2. You can freely access all interface components of the external class. This is also the two advantages of internal classes. The above code is in the form of an internal class !!

Third: the activity itself serves as the event listener

In this form, the activity itself is used as the listener class and the event processor method can be defined directly in the activity class. This form is very simple. However, this approach has two disadvantages: (1) this form may cause program structure confusion. The main responsibility of an activity is to complete interface initialization. However, the event processor method must also be included, which can cause confusion. (2) If the activity interface class needs to implement the listener interface, it is strange.

The above program enables the activity class to implement the onclicklistener event listening interface, so that you can directly define the event processor method in this activity class: onclick (view V ), when you add an event listener object to a component, you can directly use this as the event listener object.

Type 4: External class as listener

Buttontest class

When you click the button, the program will trigger the mybuttonlistener listener.

External mybuttonlistener class

It is rare to define the event listener class using top-level classes for the following two reasons:

1. The event listener is usually a specific GUI, which is defined as an external class without basketball to improve the cohesion of the program.

2. event listeners in the form of external classes cannot freely access the components in the classes that create the GUI. The programming is not concise enough.

However, if an event listener needs to be shared by multiple GUI interfaces and is mainly used to implement a certain business logic, you can consider using an external class to define the event listener class.

Category 5: bind directly to tags

Android also provides a simpler way to bind event listeners. You can bind events to specified tags directly in the interface layout file.

For many Android tags, they all support attributes such as onclick and onlongclick. The attribute value is like XXX.

(View Source)

Method Name.

Add properties for the button in the layout file, such as code:

Bind a button event handling method: clickhanlder, which means that the developer needs to define a void clickhanler (View Source) method in the activity corresponding to the page layout, this method processes the click events on this button.

The following is the Java code corresponding to the interface layout:

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.