Transfer Data Using bundle in android

Source: Internet
Author: User

Activity1.xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<AbsoluteLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/widget0"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

<TextView
Android: id = "@ + id/title"
Android: layout_width = "243px"
Android: layout_height = "29px"
Android: text = "@ string/title"
Android: textSize = "24sp"
Android: layout_x = "36px"
Android: layout_y = "32px"/>
<TextView
Android: id = "@ + id/text1"
Android: layout_width = "wrap_content"
Android: layout_height = "37px"
Android: text = "@ string/text1"
Android: textSize = "18sp"
Android: layout_x = "40px"
Android: layout_y = "156px"/>
<TextView
Android: id = "@ + id/text2"
Android: layout_width = "wrap_content"
Android: layout_height = "29px"
Android: text = "@ string/text2"
Android: textSize = "18sp"
Android: layout_x = "40px"
Android: layout_y = "102px"/>
<TextView
Android: id = "@ + id/text3"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "cm"
Android: textSize = "18sp"
Android: layout_x = "231px"
Android: layout_y = "157px"/>
<Button
Android: id = "@ + id/button1"
Android: layout_width = "70px"
Android: layout_height = "48px"
Android: layout_x = "130px"
Android: layout_y = "232px"
Android: text = "computing"/>

<RadioGroup
Android: id = "@ + id/sex"
Android: layout_width = "300px"
Android: layout_height = "100px"
Xmlns: android = "http://schemax.android.com/apk/res/android"
Android: layout_x = "97px"
Android: layout_y = "98px"
Android: orientation = "horizontal"
Android: checkedButton = "@ + id/sex1">
<RadioButton
Android: id = "@ + id/sex1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "male"/>
<RadioButton
Android: id = "@ + id/sex2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "female"/>
</RadioGroup>

<EditText
Android: id = "@ + id/height"
Android: layout_width = "130px"
Android: layout_height = "wrap_content"
Android: textSize = "18sp"
Android: layout_x = "96px"
Android: layout_y = "142px"
Android: numeric = "decimal">
</EditText>"

</AbsoluteLayout>

 

Activity2.xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<AbsoluteLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

<TextView
Android: id = "@ + id/text1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textSize = "20sp"
Android: layout_x = "50px"
Android: layout_y = "72px"/>

</AbsoluteLayout>

 

Activity1.java

Package com. bund;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. RadioButton;

Public class Activity1 extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity1 );

Button b1 = (Button) findViewById (R. id. button1 );
B1.setOnClickListener (new Button. OnClickListener (){

@ Override
Public void onClick (View v ){
// Get the entered height www.2cto.com
EditText et = (EditText) findViewById (R. id. height );
Double height = Double. parseDouble (et. getText (). toString ());
// Obtain the Selected Gender
String sex = "";
RadioButton P4 = (RadioButton) findViewById (R. id. sex1 );
If (rb1.isChecked ()){
Sex = "M ";
} Else {
Sex = "F ";
}

Intent intent = new Intent ();
Intent. setClass (Activity1.this, Activity2.class );

// New a Bundle object and pass in the data to be passed in
Bundle bundle = new Bundle ();
Bundle. putDouble ("height", height );
Bundle. putString ("sex", sex );

// Assign the Bundle object assign to Intent
Intent. putExtras (bundle );

// Call Activity2
StartActivity (intent );
}

});
}
}

 

Activity2.java

Package com. bund;

Import java. text. DecimalFormat;
Import java. text. NumberFormat;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;

Public class Activity2 extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );

// Load activity2.xml
SetContentView (R. layout. activity2 );

// Obtain the Bundle object in Intent
Bundle bunde = this. getIntent (). getExtras ();

// Obtain data in the Bundle object
String sex = bunde. getString ("sex ");
Double height = bunde. getDouble ("height ");

// Determine gender
String sexText = "";
If (sex. equals ("M ")){
SexText = "male ";
} Else {
SexText = "female ";
}

// Get the standard weight
String weight = this. getWeight (sex, height );

// Set the input text
TextView tv1 = (TextView) findViewById (R. id. text1 );
Tv1.setText ("You are a person" + sexText + "\ n your height is" + height + "CM \ n your standard weight is" + weight + "kg ");
}

// Rounding
Private String format (double num ){
Numberformatter = new DecimalFormat ("0.00 ");
String s = formatter. format (num );
Return s;
}

// Obtain the Button object with findViewById () and add onClickListener
Private String getWeight (String sex, double height ){
String weight = "";
If (sex. equals ("M ")){
Weight = format (height-80) * 0.7 );
} Else {
Weight = format (height-70) * 0.6 );
}
Return weight;
}
}

 

From Gao jicai _ Android

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.