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, 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