Receive value transfer method between Android Interfaces
Last Update:2018-12-06
Source: Internet
Author: User
Mainactivity. Java
Package com. mainactivity;Import Android. App. activity;Import Android. content. intent;Import Android. OS. Bundle;Import Android. View. view;Import Android. widget. Button;Import Android. widget. edittext;Public class mainactivity extends activity {Private button nextbutton;Private edittext nameedit;Private edittext phoneedit;@ OverridePublic void oncreate (bundle icicle ){Super. oncreate (icicle );Setcontentview (R. layout. Main );// Upload all WidgetsLoadwidget ();Nextbutton. setonclicklistener (New button. onclicklistener (){@ OverridePublic void onclick (view ){// Send data to the next actitityDelivertonextactitity ();}});}Public void delivertonextactitity (){// Create a bundle object and put the number of items to be deleted in the bundleBundle bundle = new bundle ();Bundle. putstring ("name", nameedit. gettext (). tostring ());Bundle. putstring ("phone", phoneedit. gettext (). tostring ());Intent intent = new intent ();// Set a actitityIntent. setclass (this, secondactivity. Class );Intent. putextras (bundle );// Open an activityStartactivity (intent );}Private void loadwidget (){Nextbutton = (button) findviewbyid (R. Id. nextbutton );Nameedit = (edittext) findviewbyid (R. Id. nameedit );Phoneedit = (edittext) findviewbyid (R. Id. phoneedit );}}Secondactivity. Java Package com. mainactivity;Import Android. App. activity;Import Android. OS. Bundle;Import Android. View. view;Import Android. widget. Button;Import Android. widget. textview;Public class secondactivity extends activity {@ OverridePublic void oncreate (bundle icicle ){Super. oncreate (icicle );Setcontentview (R. layout. Second );Textview content = (textview) findviewbyid (R. Id. content );Button backbutton = (button) findviewbyid (R. Id. backbutton );// Obtain the bundle object generated by the previous activityBundle bundle = getintent (). getextras ();Content. settext ("name:" + bundle. getstring ("name") + "Tel:" + bundle. getstring ("phone "));Backbutton. setonclicklistener (New button. onclicklistener (){@ OverridePublic void onclick (view ){// End the activityFinish ();}});}}Main. xml <? XML version = "1.0" encoding = "UTF-8"?><Absolutelayout xmlns: Android = "http://schemas.android.com/apk/res/android"Android: Orientation = "vertical"Android: layout_width = "fill_parent"Android: layout_height = "fill_parent"Android: Id = "@ + ID/widget149"><Textview Android: Id = "@ + ID/nametext"Android: layout_height = "wrap_content"Android: layout_width = "wrap_content"Android: layout_x = "0px"Android: layout_y = "18px"Android: text = "name:"></Textview><Edittext Android: Id = "@ + ID/nameedit"Android: layout_height = "36px"Android: layout_width = "100px"Android: layout_x = "58px"Android: layout_y = "12px"></Edittext><Textview Android: Id = "@ + ID/phonetext"Android: layout_height = "-12px"Android: layout_width = "wrap_content"Android: layout_x = "-1px"Android: layout_y = "62px"Android: text = "Tel:"></Textview><Edittext Android: Id = "@ + ID/phoneedit"Android: layout_height = "36px"Android: layout_width = "100px"Android: layout_x = "58px"Android: layout_y = "57px"></Edittext><Button Android: Id = "@ + ID/nextbutton"Android: layout_height = "wrap_content"Android: layout_width = "wrap_content"Android: layout_x = "4px"Android: layout_y = "103px"