Android Click-to-listen events are often used, often used in the following types of
First: The most commonly used, by implementing Onclicklisten, the implementation of the OnClick method to achieve
Implement the interface to achieve click button1 = (Button) Findviewbyid (R.id.button1); Button1.setonclicklistener (this);
@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.button1:toast.maketext (Mainactivity.this, "Implementing Interface Mode" , 0). Show (); break;}}
The advantage of this method is that when a page has a large number of click events, you can put the click event in the override method, avoid too much code in the OnCreate, inconvenient to read
The second kind: rewrite a class to implement the Onclicklistener interface
Rewrite a class to implement Button2 = (Button) Findviewbyid (R.id.button2); Button2.setonclicklistener (new Mybuttonclick ());
Class Mybuttonclick implements Onclicklistener {@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.butto N2:Toast.makeText (Mainactivity.this, "overriding class mode", 0). Show (); break;}}}
The Third Kind: The method of clicking the click event directly in the layout file, realizes the listener of click event through reflection
public void Btnclick (View v) { switch (V.getid ()) {case R.id.button3: toast.maketext (Mainactivity.this, " Define the click Method directly in the layout file, 0). Show (); break; } }
The fourth kind: Anonymous inner class method to implement
Anonymous inner class button4 = (Button) Findviewbyid (r.id.button4); Button4.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) { switch ( V.getid ()) {case r.id.button4: toast.maketext (mainactivity.this, "Anonymous inner class", 0). Show (); Break;}} );
See demo,:http://download.csdn.net/detail/xiaoxiaomao123098/9008249 for details.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Four click-to-Listen mode