Android transmits data between different Actitity, androidactitity

Source: Internet
Author: User

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 );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.