Directly on the code:
Public classMainactivityextendsAppcompatactivityImplementsview.onclicklistener{Button Button1,button2,button3,button4,button5,button6; TextView Text1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button1=(Button) Findviewbyid (R.id.button1); Button2=(Button) Findviewbyid (R.id.button2); Button3=(Button) Findviewbyid (R.id.button3); Button4=(Button) Findviewbyid (R.ID.BUTTON4); //Click events for Button 5 and button 6 are added in the layout filebutton5=(Button) Findviewbyid (R.ID.BUTTON5); Button6=(Button) Findviewbyid (R.ID.BUTTON6); Text1=(TextView) Findviewbyid (R.ID.TEXT1); //First buttonButton1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Text1.settext ("Button 1"); } }); //a second buttonButton2.setonclicklistener (list); //Third button to create class inherit click eventButton3.setonclicklistener (Newmm ()); //The fourth button itself inherits a click eventButton4.setonclicklistener ( This); } Button.onclicklistener List=NewButton.onclicklistener () {@Override Public voidOnClick (View v) {Text1.settext ("Button 2"); } }; classMmImplementsview.onclicklistener{@Override Public voidOnClick (View v) {Text1.settext ("Button 3"); }} @Override Public voidOnClick (View v) {Text1.settext ("Button 4"); } Public voidClick (View v) {intId=V.getid (); Switch(ID) { CaseR.id.button5:text1.settext ("Button 5"); Break; CaseR.id.button6:text1.settext ("Button 6"); Break; } }}
Layout file Code:
<linearlayout android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "Match_ Parent "xmlns:android=" http://schemas.android.com/apk/res/android "> <button android:layout_width=" match _parent "android:layout_height=" wrap_content "android:id=" @+id/button1 "android:text=" button 1 "and roid:layout_gravity= "center"/> <button android:layout_width= "Match_parent" Android:layout_h eight= "Wrap_content" android:id= "@+id/button2" android:text= "button 2" android:layout_gravity= "center" /> <button android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android Oid:id= "@+id/button3" android:text= "button 3" android:layout_gravity= "center"/> <button A Ndroid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:id= "@+id/button4" and roid:text= "Button 4" android:layout_gravity= "center"/> <button android:layout_width= "Match_parent" android:layout _height= "Wrap_content" android:id= "@+id/button5" android:text= "button 5" android:onclick= "click" an droid:layout_gravity= "center"/> <button android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content" android:id= "@+id/button6" android:text= "button 6" android:onclick= "click" and roid:layout_gravity= "center"/> <textview android:layout_width= "Match_parent" android:layout _height= "Match_parent" android:id= "@+id/text1" android:textcolor= "#FF0000" android:textsize= "30DP" android:gravity= "center" android:text= "not currently clicked"/></linearlayout>
:
Android Button click event Summary