button Click event: probably divided into the following types:
- Anonymous inner class
- Define internal classes to implement Onclicklistener interfaces
- Defining the Construction method
- Implementing Onclicklistener interfaces with activity
- Property that specifies the onclick of the button
First, 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 "/>
Then write the Java code:
1. Anonymous inner class
1 @Override2 protected voidonCreate (Bundle savedinstancestate) {3 Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.activity_main);5 6BT =(Button) Findviewbyid (R.ID.BT1);7 //1. Anonymous inner class8Bt.setonclicklistener (NewOnclicklistener () {9 Ten @Override One Public voidOnClick (View v) { A //TODO auto-generated Method Stub -LOG.I ("Anonymous inner Class", "click event"); - } the }); -}
2. Define the inner class to implement the Onclicklistener interface
1 Public classMainactivityextendsactivity{2 3 PrivateButton bt;4 5 @Override6 protected voidonCreate (Bundle savedinstancestate) {7 Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9 TenBT =(Button) Findviewbyid (R.ID.BT); One ABt.setonclicklistener (NewMyListener ()); - } - the //define internal classes to implement Onclicklistene interfaces - classMyListenerImplementsonclicklistener{ - - @Override + Public voidOnClick (View v) { - //TODO auto-generated Method Stub +LOG.I ("Define internal classes, implement Onclicklistene interfaces", "click events"); A } at } -}
3. Defining the Construction method
1 Public classMainactivityextendsactivity{2 3 PrivateButton bt;4 5 @Override6 protected voidonCreate (Bundle savedinstancestate) {7 Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9 TenBT =(Button) Findviewbyid (R.ID.BT); One A MyListener (); - } - the //Defining construction Methods - Private voidMyListener () { - //TODO auto-generated Method Stub -Bt.setonclicklistener (NewOnclicklistener () { + - @Override + Public voidOnClick (View v) { A //TODO auto-generated Method Stub atLOG.I ("Define constructor Method", "click event"); - } - }); - } -}
4. Implement Onclicklistener interface with activity
1 Public classMainactivityextendsActivityImplementsOnclicklistener {2 3 PrivateButton bt;4 5 @Override6 protected voidonCreate (Bundle savedinstancestate) {7 Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9 TenBT =(Button) Findviewbyid (R.ID.BT); OneBt.setonclicklistener ( This); A } - - the //implementing Onclicklistener interfaces with activity - @Override - Public voidOnClick (View v) { - //TODO auto-generated Method Stub +LOG.I ("Implement Onclicklistener Interface with activity", "click event"); - } + A}
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:
1 < Button 2 3 Android:layout_width = "Wrap_content" 4 android:layout_height= "Wrap_content"5 Android:onclick = "click" 6 android:text= "click event"/>
Java code:
1 Public classMainactivityextendsActivity {2 3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 8 }9 Ten //5 specifying the onclick attribute mode One Public voidClick (View v) { A //TODO auto-generated Method Stub -LOG.I ("Specify the onclick attribute mode", "click event"); - } the}
In addition, multiple button click event processing, to specify the onclick attribute mode as an example, get its resource ID, through the resource ID, you can determine which button the user clicked.
Layout file:
<ButtonAndroid:id= "@+id/bt1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Click event 1" /> <ButtonAndroid:id= "@+id/bt2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Click event 2" /> <ButtonAndroid:id= "@+id/bt3"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Click event 3" />/><ButtonAndroid: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; } }}
Android--android Studio--button Click event notation