Android transmits data between different Actitity, androidactitity
This article implements a simple character calculator to transfer data between different Actitity
Intent Data Transmission
Open interface B from interface A and pass the data on interface A to interface B.
1. intent. setData (uri) -- intent. getData ();
You can transmit simple text data.
2. intent. putExtra (name, value)
8 basic data types and arrays can be passed
The String object can pass charSequence.
Serializable object (serialized to a file) can also be passed
The serializable object (serialized to memory) Parcelable can also pass bitmap
You can pass a map set Bundle extras = new Bundle ();
URL: http://www.cnblogs.com/wuyudong/p/5679191.html.
The code in activity_main.xml is as follows:
<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: orientation = "vertical" tools: context = ". mainActivity "> <TextView android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: gravity =" center_horizontal "android: text =" character calculator "android: textColor = "# ff0000" android: textSize = "26sp"/> <EditText android: id = "@ + id/et_name" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "enter a name"> </EditText> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "match_parent"> <Button android: layout_centerInParent = "true" android: onClick = "click" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "computing"> </Button> </RelativeLayout> </LinearLayout>
The interface is as follows:
Enter the name and click the button to go to another result page: activity_result.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Test Result:" android: textColor = "# ff0000" android: textSize = "20sp"/> <TextView android: id = "@ + id/TV _result" android: text = "xxx:" andro Id: layout_width = "match_parent" android: layout_height = "wrap_content" android: textColor = "#99000000" android: textSize = "17sp"/> <ProgressBar android: id = "@ + id/progressBar1" android: max = "100" style = "? Android: attr/progressBarStyleHorizontal "android: layout_width =" fill_parent "android: layout_height =" wrap_content "/> </LinearLayout>
The interface is as follows:
The ResultActivity. java code is as follows:
Package com. wuyudong. testrp; import java. util. random; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. progressBar; import android. widget. textView; public class ResultActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_result); TextView TV _result = (TextView) findViewById (R. id. TV _result); Intent intent = getIntent (); String name = intent. getStringExtra ("name"); Random rm = new Random (); int rp = rm. nextInt (101); TV _result.setText (name + ": Your character value is:" + rp); ProgressBar pb = (ProgressBar) findViewById (R. id. progressBar1); pb. setProgress (rp );}}