in the information age, people do not have to fly as the ancient people like to preach books, any text message, QQ, Weibo can and their dear little friends to communicate quickly. In such a dazzling information age, it is necessary to choose a suitable communication tool, the activity in Android and activity between the method is also a lot of, in the project how to choose the information interaction method, look at the project needs and their own experience.
One thing you need to know before you get an example: Intent;
Books and online see the concept of interpretation are many, but I like its English meaning: intentions. What you want to do, tell it. For example, I want to pass the argument, then you set up in this intent, to the second interface through the intention that you set to pass the argument, can parse out. For example, you want to open the interface, you set the intent inside the interface parameters to open , and then through the method call can be. Or a simpler explanation is the transfer!
Begin to understand the actual combat:
1. Create a new project: Activityintent
2. According to the previous article, create another interface, the file created is: Activity_first.xml and firstactivity
The code for the interface layout in Activity_first.xml is as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " > <edittext android:id=" @+id/text " Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "" android: Layout_centerhorizontal= "true"/> <button android:id= "@+id/close_1" android:layout_below= "@ Id/text " android:layout_width=" fill_parent " android:layout_height=" wrap_content " android:text=" Close the /></relativelayout>
The main interface layout file code is as follows:
<relativelayout 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 = "${relativepackage}.${activityclass}" > <textview android:id= "@+id/title" android:layout_width= "WR Ap_content "android:layout_height=" wrap_content "android:text=" interface between "Android:layout_centerhorizonta" L= "true"/> <button android:id= "@+id/open_1" android:layout_below= "@id/title" android:layou T_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "intent (first type)"/> <button android:id= "@+id/open_2" android:layout_below= "@id/open_1" android:layout_width= "fi Ll_parent "android:layout_height=" wrap_content "android:text=" Intent parameters (second type) "/> < ; Button android:id= "@+id/open_3" android:layout_below= "@id/open_2" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:text= "Intent (third type)"/></relativelayout>
3. Add the new interface layout to the program, modify the next androidmanifest.xml, before the </application> node added the following::
<activity android:name= ". Firstactivity " android:label=" @string/app_name "> </activity>
4. Write the entry code mainactivity as follows:
Package Com.wyz.activityintent;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.toast;public Class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Button intentbtn = (button) Findviewbyid (r.id.open_1); Button intentbtn_2 = (button) Findviewbyid (r.id.open_2); Button Intentbtn_3 = (button) Findviewbyid (r.id.open_3); Intentbtn.setonclicklistener (new Btnlistener ()); Intentbtn_ 2.setOnClickListener (New Btnlistener ()); Intentbtn_3.setonclicklistener (new Btnlistener ());} Class Btnlistener implements Onclicklistener {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (mainactivit Y.this, Firstactivity.class); switch (V.getid ()) {case r.id.open_1://The first type of Intent.putextra ("name", "wyz_1_"); Intent.putextra ("Age", 18); StartActivity (Intent); Break Case r.id.open_2://Second BUNDLE bundle = new bundle (); Bundle.putstring ("name", "wyz_2_"); Bundle.putint ("Age", 27); Intent.putextras (bundle); StartActivity (Intent); Break Case R.id.open_3://Third Intent.putextra ("name", "wyz_3_"); Intent.putextra ("Age", 35); Startactivityforresult (Intent, 0); Break Default:break; }}} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (Requestcode = = 0)// This request code corresponds to Startactivityforresult (intent, 0); {if (ResultCode = = 1)//This request code corresponds to open Interface Setresult (1, resultintent); {String Sret = Data.getstringextra ("return"); Toast.maketext (Getapplicationcontext (), Sret, Toast.Length_long). Show ();; }}super.onactivityresult (Requestcode, ResultCode, data);}}
4. Write another interface business code firstactivity as follows:
Package Com.wyz.activityintent;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class Firstactivity extends Activity {int itype=0;//judgment is the first method triggered, default 0@overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_first); EditText Text = (EditText) Findviewbyid (R.id.text); Button close_btn = (button) Findviewbyid (r.id.close_1); Close_btn.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {if (IType = = 3)//equals third, closes the interface return parameter {Intent resultintent = new Intent (); Resultintent.putextra ("Return", "Hello Wyz"); Setresult (1, resultintent); FirstActivity.this.finish (); }else{firstactivity.this.finish ();} FirstActivity.this.finish ();}}); I Ntent intent = getintent (); StrinG SName = Intent.getstringextra ("name"),//int iage = Intent.getintextra ("Age", 0); Gets the parameter of the first int iage = Intent.getextras (). GetInt ("age");//Gets the parameter the second String SAge = string.valueof (iage); Text.settext (sname+ SAGE);//Set display result if (Sname.compareto ("wyz_3_") = = 0) {iType = 3;}}}
Actual combat effect:
"Android Development-9" Information age, how activity and activity communicate