Talk briefly about the five listener events for button in Android:
1. Add the OnClick property to the button in the layout file, and the activity implements its method
2. Anonymous inner class as event listener class
3. Internal classes as listeners
4.Activity itself as an event listener to implement Onclicklistener
5. External classes as listeners
Buttonlisteneractivity.class
public class Buttonlisteneractivity extends appcompatactivity implements view.onclicklistener{private Button BT_ONE,BT _two,bt_three,bt_four,bt_five; protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_button_listener); Bt_two = (Button) Findviewbyid (r.id.bt_listener_two); Bt_three = (Button) Findviewbyid (R.id.bt_listener_three); Bt_four = (Button) Findviewbyid (R.id.bt_listener_four); Bt_five = (Button) Findviewbyid (r.id.bt_listener_five); Mode two: Anonymous inner class as Event listener Class Bt_two.setonclicklistener (new View.onclicklistener () {@Override public V OID OnClick (View v) {toast.maketext (Buttonlisteneractivity.this, "Way II: Anonymous inner Class", Toast.length_long). Show (); } }); Mode three: Inner class as Event listener class MyButton listener = new MyButton (this); Bt_three.setonclicklistener (listener); Mode four: Activity itself as an event listener to achieve Onclicklistener Bt_four.setonclicklistener (this); Mode five: External class as Event listener Class Bt_five.setonclicklistener (new Mybuttonlistener (this)); //1. Mode one: Add the OnClick property to the button in the layout file, and the activity implements its method public void ButtonClick (view view) {Toast.maketext (buttonlis Teneractivity.this, "Way One: OnClick", Toast.length_long). Show (); }//Mode four: Activity itself as an event listener, implement onclicklistener--override OnClick () @Override public void onclick (View v) {Toast.mak EText (Buttonlisteneractivity.this, "way four: Activity itself as an event listener", Toast.length_long). Show (); }//Mode three: Inner class as Event listener class MyButton implements view.onclicklistener{private context context; Public MyButton (Context context) {This.context = context; } @Override public void OnClick (View v) {toast.maketext (context, "Way three: Inner class", Toast.length_long). Sho W (); } }}
Activity_button_listener.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:orientation= "vertical" tools:context= " Com.langdon.taiyang.androidtest.button.ButtonListenerActivity "><button android:id=" @+id/bt_listener_one " Android:text= "Way One: OnClick binding" android:onclick= "ButtonClick" android:layout_width= "Match_parent" Android:layout_hei ght= "Wrap_content"/> <button android:id= "@+id/bt_listener_two" android:text= "way Two: Anonymous inner class" and Roid:layout_width= "Match_parent" android:layout_height= "wrap_content"/> <button android:id= "@+id/b T_listener_three "android:text=" mode three: internal class monitoring "android:layout_width=" Match_parent "android:layout_height= "Wrap_content"/> <button android:id= "@+id/bt_listener_four" Android:text= "mode four: Activity itself realizes the listener event" android:layout_width= "Match_parent" android:layout_height= "Wrap_content"/> <button android:id= "@+id/bt_listener_five" android:text= "Way five: External class as listener" android:layout_width= "Match_ Parent "android:layout_height=" Wrap_content "/></linearlayout>
Mybuttonlistener.class
public class Mybuttonlistener implements View.onclicklistener { private context context; Public Mybuttonlistener (Context context) { This.context = context; } @Override public void OnClick (View v) { Toast.maketext (context, "Way V: External class", Toast.length_long). Show (); }}
Five kinds of listener events for button in Android