PackageCom.geli_2.sujie.sujiegeili2testbutton;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsAppcompatactivityImplementsview.onclicklistener{PrivateButton Mbtclick; PrivateButton MBtClick2; PrivateButton MBtClick3; PrivateButton MBtClick4; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mbtclick= (Button) Findviewbyid (R.ID.BTNTEST1);//1. FDV Find button ID//2. Alt Ctrl F to form a class global fieldMbtclick.setonclicklistener (NewView.onclicklistener ()//3. Type this line of code to embed the inner class method automatically{@Override Public voidOnClick (View v) {//4. Toast tab to bring up Toast method above for method oneToast.maketext (mainactivity. This, "Hello World", Toast.length_short). Show (); } }); MBtClick2= (Button) Findviewbyid (r.id.btnsample2);//5. Find the button ID of button 2 fdvMbtclick2.setonclicklistener (NewButton_2_onclicklistener ());//9, instantiation method to the button binding above is method twoMBtClick3 = (Button) Findviewbyid (r.id.btntry3);//10. Find the button ID of button 3 FDVMbtclick3.setonclicklistener ( This);//11, this means main acitivity//12 But here is the method parameter is the Onclicklistener interface, where this Alt Enter is added to the implemented interface on this methodMBtClick4 = (Button) Findviewbyid (R.ID.BTNEXAMPLE4);//5. Find the button ID of button 4 fdv } /*** Called when a view has been clicked. * * @paramv The view that is clicked. */@Override Public voidOnClick (View v) {//Method Three } classButton_2_onclicklistenerImplementsView.onclicklistener//6. Set a click method to inherit from view after naming//Onclicklistener//7, after the inheritance implements ALT + ENTER to implement the method { /*** Called when a view has been clicked. * * @paramv The view that is clicked. */@Override Public voidOnClick (View v) {//Method Two } } Public voidClick (View view) {//method Four }}
Android Studio Button button Four ways to bind events