Getting Started with Android 003 (click events)

Source: Internet
Author: User

There are four ways to implement a click event.

1. Inner class Implementation method

1.0

 Packagecn.rfvip.clickevent;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                  Setcontentview (R.layout.activity_main); //First button click Inner class implementationButton bt1=(Button) Findviewbyid (R.ID.BTN1); Bt1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO Auto-generated method stubsSYSTEM.OUT.PRINTLN ("first button click (Mode one: inner class implementation)");            }        }); }    }

1.1. One of the variants of the wording

 Packagecn.rfvip.clickevent;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                  Setcontentview (R.layout.activity_main); //First button click Inner class implementationButton bt1=(Button) Findviewbyid (R.ID.BTN1); Bt1.setonclicklistener (NewMybtn1 ()); }     classMybtn1Implementsonclicklistener{@Override Public voidOnClick (View v) {//TODO Auto-generated method stubsSYSTEM.OUT.PRINTLN ("first button click (Mode one: internal class variants)"); }}   }

2. This implementation

 Packagecn.rfvip.clickevent;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivityImplementsonclicklistener{@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                  Setcontentview (R.layout.activity_main); //The second button clicks the Inner class implementationButton bt2=(Button) Findviewbyid (R.ID.BTN2); Bt2.setonclicklistener ( This); } @Override Public voidOnClick (View v) {//TODO Auto-generated method stubsSystem.out.println ("Second button click, Way two: This implementation implements Onclicklistener"); }    }

3, no ID implementation

android:onclick= "Getmeth" in Activity_main.xml

   < Button                Android:layout_width = "Wrap_content"         android:layout_height= "Wrap_content"        android:text= "Click 5 No ID implementation"         android:onclick= "Getmeth"          />

In the Mainactivity.java

 Public void Getmeth (View v)    {        System.out.println ("not Id implements  ");            }

Getting Started with Android 003 (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.