Http://www.jb51.net/article/55329.htm "Quote from"
Many people who study Android programming find that each person has different preferences for how the code is written, and what is more obvious is the difference in how the control responds to events. Therefore this article summarizes these writing, compares each kind of writing the merits and demerits, hoped that everybody flexibly chooses the code way may have the reference value.
The XML file code is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 |
<button Android:id= "@+id/button1" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Button1"/> <button Android:id= "@+id/button2" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Button2"/> |
The four methods are described as follows:
Anonymous inner class:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public class Testbuttonactivity extends Activity { Button btn1, btn2; Toast TST; @Override protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_test_button); BTN1 = (Button) Findviewbyid (R.id.button1); BTN2 = (Button) Findviewbyid (R.id.button2); Btn1.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) { TODO auto-generated Method Stub Toast TST = Toast.maketext (Testbuttonactivity.this, "111111111", Toast.length_short); Tst.show (); } }); Btn2.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) { TODO auto-generated Method Stub Toast TST = Toast.maketext (Testbuttonactivity.this, "222222222", Toast.length_short); Tst.show (); } }); } } |
Custom Click event Listener class:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class Testbuttonactivity extends Activity { Button btn1, btn2; Toast TST; Class Myclicklistener implements Onclicklistener { @Override public void OnClick (View v) { TODO auto-generated Method Stub Switch (V.getid ()) { Case R.id.button1: TST = Toast.maketext (Testbuttonactivity.this, "111111111", Toast.length_short); Tst.show (); Break Case R.id.button2: TST = Toast.maketext (Testbuttonactivity.this, "222222222", Toast.length_short); Tst.show (); Break Default Break } } } @Override protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_test_button); BTN1 = (Button) Findviewbyid (R.id.button1); BTN2 = (Button) Findviewbyid (R.id.button2); Btn1.setonclicklistener (New Myclicklistener ()); Btn2.setonclicklistener (New Myclicklistener ()); } } |
Activity inherits View.onclicklistener, which is implemented by the activity onclick (view view) method, in the onclick (view view) The method uses Switch-case to handle the button corresponding to different ID.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 |
public class Testbuttonactivity extends Activity implements Onclicklistener { Button btn1, btn2; Toast TST; @Override protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_test_button); BTN1 = (Button) Findviewbyid (R.id.button1); BTN2 = (Button) Findviewbyid (R.id.button2); Btn1.setonclicklistener (this); Btn2.setonclicklistener (this); } @Override public void OnClick (View v) { TODO auto-generated Method Stub Switch (V.getid ()) { Case R.id.button1: TST = Toast.maketext (This, "111111111", Toast.length_short); Tst.show (); Break Case R.id.button2: TST = Toast.maketext (This, "222222222", Toast.length_short); Tst.show (); Break Default Break } } } |
The last one I see today is a notation that "displays the OnClick property of a specified button in an XML file, so that when the button is clicked, the Click () method in the corresponding activity is invoked using reflection."
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
<button android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:onclick= ' OnClick ' android:text= ' Button1 '/> < Button android:id= "@+id/button2" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:onclick= "OnClick" android:text= "Button2"/> |
Here in the end of the android: Press alt+/will have the OnClick property prompt, but the input to android:onclick= "place pressed alt+/and did not prompt the OnClick option, let me suddenly feel there seems to be a problem.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 |
public class Testbuttonactivity extends Activity { Button btn1, btn2; Toast TST; @Override protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_test_button); } Notice there's no @Override tag here. public void OnClick (View v) { TODO auto-generated Method Stub Switch (V.getid ()) { Case R.id.button1: TST = Toast.maketext (This, "111111111", Toast.length_short); Tst.show (); Break Case R.id.button2: TST = Toast.maketext (This, "222222222", Toast.length_short); Tst.show (); Break Default Break } } } |
In this case, the button's Click event can be implemented without declaring the button in the entire code.
These are the four ways to implement a button click event.
A rough summary, that is, when the button is less time with anonymous internal classes will be faster, such as writing a demo test or landing interface and so on.
Button A lot of cases I still choose a third method, convenient.
About the fourth method, I feel the most convenient, but read a lot of code or feel that the wording is not popular, interested friends can study this. I believe there will be a lot of gains.
Four common ways to summarize Android button click events