The Mainactivity.java code is as follows:
PackageCom.oazzz.test9;Importandroid.support.annotation.Nullable;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener {//Several implementation methods of event listener//1. Internal class Form//2. External class Form//3. Activity itself as an event listener class//4. Anonymous internal class form@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView C1=(TextView) Findviewbyid (R.ID.C1); C1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Toast.maketext (View.getcontext (),"Click on the first line of text! ", Toast.length_long). Show (); } }); TextView C2=(TextView) Findviewbyid (R.ID.C2); C2.setonclicklistener (NewButtonclicklistener ()); TextView C3=(TextView) Findviewbyid (R.ID.C3); C3.setonclicklistener ( This); } @Override Public voidOnClick (view view) {Toast.maketext (View.getcontext (),"Click on the third line of text! ", Toast.length_long). Show (); } Public voidClickHandler (view view) {Toast.maketext (View.getcontext (),"Click on the fourth line of text! ", Toast.length_short). Show (); } Private Final classButtonclicklistenerImplementsview.onclicklistener{@Override Public voidOnClick (view view) {Toast.maketext (View.getcontext (),"Clicked on the second line of text! ", Toast.length_short). Show (); } }}The Activity_main.xml code is as follows:
<?XML version= "1.0" encoding= "Utf-8"?><Android.support.constraint.ConstraintLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.oazzz.test9.MainActivity"> <TextViewAndroid:id= "@+id/c1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/c1"App:layout_constraintbottom_tobottomof= "Parent"App:layout_constraintleft_toleftof= "Parent"App:layout_constraintright_torightof= "Parent"App:layout_constrainttop_totopof= "Parent"App:layout_constraintvertical_bias= "0.2" /> <TextViewAndroid:id= "@+id/c2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/c2"App:layout_constraintbottom_tobottomof= "Parent"App:layout_constraintleft_toleftof= "Parent"App:layout_constraintright_torightof= "Parent"App:layout_constrainttop_totopof= "Parent"App:layout_constraintvertical_bias= "0.4" /> <TextViewAndroid:id= "@+id/c3"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/c3"App:layout_constraintbottom_tobottomof= "Parent"App:layout_constraintleft_toleftof= "Parent"App:layout_constraintright_torightof= "Parent"App:layout_constrainttop_totopof= "Parent"App:layout_constraintvertical_bias= "0.6" /> <TextViewAndroid:id= "@+id/c4"Android:onclick= "ClickHandler"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/c4"App:layout_constraintbottom_tobottomof= "Parent"App:layout_constraintleft_toleftof= "Parent"App:layout_constraintright_torightof= "Parent"App:layout_constrainttop_totopof= "Parent"App:layout_constraintvertical_bias= "0.8" /></Android.support.constraint.ConstraintLayout>
Android-studio automatically generate overloaded code menu items in Generate, Override Methods ...
[Android] Development Day 11th