Android -- Activity carries data during jump
First, let's take a look at two transfer method examples: (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 savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View v) {Intent intent = new Intent (this, SecondActivity. class); // encapsulate data into an intent object // intent. putExtra ("malename", "Michael"); // intent. putExtra ("femalename", "Sister Furong"); // encapsulate data into bundle objects. Bundle bundle = new Bundle (); bundle. putString ("malename", "Michael"); bundle. putString ("femalename", "Sister Furong"); // encapsulate the bundle object into the intent object. 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 (the Marriage value of maleName + "and" + feMaleName + "is" + 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: paddingbottom = "@ dimen/activity_vertical_margin" android: paddingleft = "@ dimen/plugin" android: paddingright = "@ dimen/plugin" android: paddingtop = "@ dimen/plugin" tools: context = ". mainActivity "android: orientation =" vertical "> <textview android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" this is a 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" android: layout_height = "wrap_content" android: text = "Sister Furong"> <button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "computing" android: onclick = "click"> </button> </edittext> </textview> </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/plugin" android: paddingright = "@ dimen/plugin" android: paddingtop = "@ dimen/plugin" tools: context = ". mainActivity "> <textview android: id =" @ + id/TV "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "this is the second Activity"> </textview> </relativelayout>