First look at two examples of delivery methods: (A simple marriage calculator)
Main activity
Import Android.os.bundle;import android.app.activity;import Android.content.intent;import Android.view.Menu;import Android.view.view;public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstanc EState) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); public void Click (View v) {Intent Intent = new Intent (this, secondactivity.class); The data is encapsulated into the intent object//Intent.putextra ("Malename", "Zhang San");//Intent.putextra ("Femalename", "Sister Furong"); Encapsulate data into Bundle object Bundle bundle = new bundle (); Bundle.putstring ("Malename", "<span style=" font-family:arial, Helvetica, Sans-serif; " > Zhang San </span> "<span style=" font-family:arial, Helvetica, Sans-serif; " >);</span> bundle.putstring ("Femalename", "Sister Furong"); Encapsulates the bundle object into the intent object Intent.putextras (bundle); StartActivity (Intent); } }
Import Java.util.random;import android.app.activity;import Android.content.intent;import Android.os.Bundle;import Android.widget.textview;public class Secondactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_second); Intent Intent = Getintent ();//extract the encapsulated data from the Intent object//string malename = Intent.getstringextra ("Malename");//string femalename = Intent.getstringextra ("Femalename"); Bundle bundle = Intent.getextras (); String malename = bundle.getstring ("Malename"); String femalename = bundle.getstring ("Femalename"); Random rd = new random (), int yinyuan = rd.nextint (100); TextView TV = (TextView) Findviewbyid (r.id.tv); Tv.settext (Malename + "and" + Femalename + "have the marriage value" + Yinyuan);}}
Activity_main.xml:
<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 "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "android:orientation=" vertical "> <textview android:layout_width=" wrap_content " android:layout_height= "Wrap_content" android:text= "This is the marriage calculator, very accurate yo"/> <edittext android:id= "@+id/et _malename "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:text=" Zhang San " /> <edittext android:id= "@+id/et_femalename" android:layout_width= "Match_parent" an droid:layout_height= "Wrap_content" Android:text= "Sister Furong"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text= "Calculate" android:onclick= "click"/></linearlayout>
Activity_second.xml:
<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 " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <textview android:id=" @+id/tv " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "This is a second activity"/></relativelayout>
Android--activity to carry data when jumping