Android Note--button Click event several ways to write

Source: Internet
Author: User

button Click event: probably divided into the following types:

    1. Anonymous inner class
    2. Define internal classes to implement Onclicklistener interfaces
    3. Defining the Construction method
    4. Implementing Onclicklistener interfaces with activity
    5. Property that specifies the onclick of the button

First, we simply define an XML layout file with a button

Activity_main.xml:

< Button         Android:id = "@+id/bt1"         android:layout_width= "Wrap_content"        android:layout_height= "Wrap_ Content "        android:text=" click event "/>

And then write the Java code.

Mainactivity.java:

1. Anonymous inner class

  

@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); BT=(Button) Findviewbyid (R.ID.BT1); //1. Anonymous inner classBt.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubLOG.I ("Anonymous inner Class", "click event"); }        });
}

2. Define the inner class to implement the Onclicklistener interface

  

 Public classMainactivityextendsActivity{    PrivateButton BT; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); BT=(Button) Findviewbyid (R.ID.BT); Bt.setonclicklistener (NewMyListener ()); }        //define internal classes to implement Onclicklistene interfaces    classMyListenerImplementsonclicklistener{@Override Public voidOnClick (View v) {//TODO auto-generated Method StubLOG.I ("Define internal classes, implement Onclicklistene interfaces", "click events"); }    }
}

3. Defining the Construction method

  

 Public classMainactivityextendsactivity{PrivateButton BT; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); BT=(Button) Findviewbyid (R.ID.BT);    MyListener (); }        //Defining construction Methods        Private voidMyListener () {//TODO auto-generated Method StubBt.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubLOG.I ("Define constructor Method", "click event");    }        }); }}

4. Implement Onclicklistener interface with activity

  

 Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateButton BT; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); BT=(Button) Findviewbyid (R.ID.BT); Bt.setonclicklistener ( This); }//implementing Onclicklistener interfaces with activity@Override Public voidOnClick (View v) {//TODO auto-generated Method StubLOG.I ("Implement Onclicklistener Interface with activity", "click event"); }    }

5. Specify the properties of the button's onclick:

First specify the OnClick property in the layout file, and then implement the OnButtonClick method in the activity

Layout file:

    < Button         android:layout_width= "Wrap_content"        android:layout_height= "Wrap_ Content "        android:onclick=" click "        android:text=" Hit event "  />

Java code:

 public  class  mainactivity extends   Activity {@Override  protected< /span> void   OnCreate (Bundle savedinstancestate { super  .oncreate (Savedinstancestate)        ;            Setcontentview (R.layout.activity_main);  // 5 specifies the onclick attribute way  public   Click (View v) { //  TODO auto-generated method stub  log.i ("Specify onclick Property Mode", "click event" ); }}

Alternatively, if multiple buttons are clicked, you can also specify the onclick attribute method to get its resource ID. With its resource ID, you can tell which button the user clicked.

  

Layout file:

<Button Android:id= "@+id/bt1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Click event 1"/> <Button Android:id= "@+id/bt2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Click event 2"/> <Button Android:id= "@+id/bt3"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Click event 3"/>/> <Button Android:id= "@+id/bt4"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Click event 4"/>

Java code:

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);            Setcontentview (R.layout.activity_main); }    //specifying the onclick property mode//the View object of the descendant is the button object that the user pressed.     Public voidClick (View v) {//TODO auto-generated Method Stub//gets the resource ID of the component        intID =V.getid (); Switch(ID) { CaseR.ID.BT1:LOG.I ("Specify the onclick attribute mode", "BT1 Click event");  Break;  CaseR.ID.BT2:LOG.I ("Specify the onclick attribute mode", "BT2 Click event");  Break;  CaseR.ID.BT3:LOG.I ("Specify the onclick attribute mode", "BT3 Click event");  Break;  CaseR.ID.BT4:LOG.I ("Specify the onclick attribute mode", "Bt4 Click event");  Break; default:             Break; }            }}

Effect

Android Note--button Click event several ways to write

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.