The android interface consists of activities.
When you create a new Android interface, an activity is automatically created. Activity can be understood as a form. Put some controls on it to form a general program.
If you want to place controls in the activity interface, you must first put them in the XML file in res-> Layout-> under the project directory. an XML file can correspond to an activity, it can also correspond to multiple activities. It can be understood that the XML file in layout is dedicated to managing the activity control.
Pay attention to the layout method used in the XML file, because the Android: Orientation = "vertical" is missing, and the interface cannot be as expected, so Mark it here. The XML file can also be switched to the visual interface (Note: It was a visual interface when it was opened for the first time). At the beginning, we should use XML for layout, it can help you understand the program.
For a newly created activity, right-click the package name and choose create class. Then let this class inherit the activity (Note: it is best to use the IDE smart prompt so that the class will be automatically imported into the activity, and then rewrite the on create (bundle) function)
After creating an activity, it is best to create a corresponding XML file under layout. The newly created XML file is blank, you can copy the content of the first XML file and modify it to the control you want to add.
After creating the activity, you must go to androidmanifest in the project. the XML file is registered in androidmanifest. add a new <activity> </activity>
Then, change Android: name to the path of this class (Note: You can copy the content of the first activity node and directly modify the path of the new activity based on the path)
The first pair of activity nodes will contain a pair of <intent-filter> </intent-filter> tags. The video is not described in detail, only the <activity> </activity> with this pair of nodes will be started by the first activity.
Communication between activities ------ intent
Communication can be performed between activities. Later, the code in an addition program is embodied in the Code.
For button events, Android is called a listener. Internal classes must be used to implement the onclicklistener interface.
The following code is a simple addition program code (Communication Between layout, button listener, and activity)
Code related to the main activity:
Import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. textview; public class mainactivity extends activity {private edittext firstedittext; private textview mytextview; private edittext secondedittext; private button mybutton; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); firstedittext = (edittext) findviewbyid (R. id. firstedittext); mytextview = (textview) findviewbyid (R. id. mytextview); mytextview. settext ("plus"); secondedittext = (edittext) findviewbyid (R. id. secondedittext); mybutton = (button) findviewbyid (R. id. mybutton); mybutton. settext ("click"); // bind the button to the listener event. setonclicklistener (New mybuttonlistener ();} // listener Interface Class mybuttonlistener implements onclicklistener {@ overridepublic void onclick (view V) {// todo auto-generated method stubstring first = firstedittext. gettext (). tostring (); string second = secondedittext. gettext (). tostring (); // communication between activities --- intent instance intent = new intent (); // create an intent instance // intent provides a key-Value Pair intent. putextra ("first", first); intent. putextra ("second", second); // The following statement can be understood as the intent from which (the first parameter) the intent is passed to (the second parameter) the activity. setclass (mainactivity. this, result. class); // select the activitymainactivity to start. this. startactivity (intent );}}}
The code for configuring the XML file for the control of the main activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" ><EditText android:id="@+id/firstEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" /><TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /><EditText android:id="@+id/secondEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" /><Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
Code of the activity that displays the result:
Import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textview; public class result extends activity {private textview mytextview = NULL; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. result_main); mytextview = (textview) findviewbyid (R. id. mytextview2); // create an intent instance to receive the data intent = getintent () received from the previous activity (); // obtain the passed key-Value Pair string first = intent. getstringextra ("first"); string second = intent. getstringextra ("second"); int myfirst = integer. parseint (first); int mysecond = integer. parseint (second); mytextview. settext (myfirst + mysecond) + "");}}
Code related to the xml configuration of the display result control:
<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" tools:context=".Result" ><TextView android:id="@+id/myTextView2" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
The result is as follows: