Four ways to do Android click events

Source: Internet
Author: User

four ways to read Android Click events:

1. Implement the Onclicklistener interface in the form of an internal class, define the Click event

Class mainactivity extents activity{//... private class MyListener implements Onclicklistener{public void Onclick (View v) {// ... Click the response of the event}}}


2. Using Anonymous internal classes
Cons: Too many anonymous internal classes are required if there are too many click events
Class mainactivity extents activity{protected void onCreate (...) {//The Bt_call.setonclicklistener (new onclicklistener{public void Onclick (View v) {//...) is implemented as an anonymous inner class. Click the response of the event}});}}


3. Implement the Mainactivity Onclicklistener interface, a very common notation in practical development
Pros: You can solve the problem with method 2
Class mainactivity extents Activity implements onclicklistener{protected void OnCreate (...) {// ...} /*** Click event */public void OnClick (View v) {//based on different Id to determine what action to complete switch (V.getid ()) {case r.id.bt_call:callphone ();//Custom function, Make a call to break;

4. The method of binding a click event in the configuration file, not commonly used
Activity_main.xml:
<button android:onclick= "Callphone"//The Click event that binds the button will automatically find the method in Activity .../>

Mainactivity.java:
Class Mainactivity extents Activity {/*** Click event, in which case the method must have parameter View v*/public void Callphone (View v) {//...}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Four ways to read Android Click events

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.