android中的bundle傳送資料

來源:互聯網
上載者: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="計算"
/>

<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="男的"
/>
<RadioButton
android:id="@+id/sex2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女的"
/>
</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) {
//取得輸入的身高
EditText et =
(EditText) findViewById(R.id.height);
double height =
Double.parseDouble(et.getText().toString());
//取得選擇的性別
String sex =
"";
RadioButton rb1 = (RadioButton)
findViewById(R.id.sex1);
if(rb1.isChecked()){
sex = "M";
}else{
sex
= "F";
}

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

//new
一個Bundle對象,並將要傳遞的資料傳入
Bundle bundle = new
Bundle();
bundle.putDouble("height", height);
bundle.putString("sex",
sex);

//將Bundle對象assign給Intent
intent.putExtras(bundle);

//調用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);

//載入activity2.xml
setContentView(R.layout.activity2);

//得Intent中的Bundle對象
Bundle
bunde = this.getIntent().getExtras();

//取得Bundle對象中的資料
String sex =
bunde.getString("sex");
double height =
bunde.getDouble("height");

//判斷性別
String sexText = "";
if(
sex.equals("M") ){
sexText = "男性";
}else{
sexText =
"女性";
}

//取得標準體重
String weight = this.getWeight(sex,
height);

//設定輸入的文字
TextView tv1 = (TextView)
findViewById(R.id.text1);
tv1.setText("你是一位" + sexText + "\n你的身高是" + height +
"厘米\n你的標準體重是"+weight+"公斤");
}

//四捨五入
private String format(double
num){
NumberFormat formatter = new DecimalFormat("0.00");
String s =
formatter.format(num);
return
s;
}

//以findViewById()取得Button對象,並添加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;
}
}

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="計算"
/>

<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="男的"
/>
<RadioButton
android:id="@+id/sex2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女的"
/>
</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) {
//取得輸入的身高
EditText et =
(EditText) findViewById(R.id.height);
double height =
Double.parseDouble(et.getText().toString());
//取得選擇的性別
String sex =
"";
RadioButton rb1 = (RadioButton)
findViewById(R.id.sex1);
if(rb1.isChecked()){
sex = "M";
}else{
sex
= "F";
}

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

//new
一個Bundle對象,並將要傳遞的資料傳入
Bundle bundle = new
Bundle();
bundle.putDouble("height", height);
bundle.putString("sex",
sex);

//將Bundle對象assign給Intent
intent.putExtras(bundle);

//調用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);

//載入activity2.xml
setContentView(R.layout.activity2);

//得Intent中的Bundle對象
Bundle
bunde = this.getIntent().getExtras();

//取得Bundle對象中的資料
String sex =
bunde.getString("sex");
double height =
bunde.getDouble("height");

//判斷性別
String sexText = "";
if(
sex.equals("M") ){
sexText = "男性";
}else{
sexText =
"女性";
}

//取得標準體重
String weight = this.getWeight(sex,
height);

//設定輸入的文字
TextView tv1 = (TextView)
findViewById(R.id.text1);
tv1.setText("你是一位" + sexText + "\n你的身高是" + height +
"厘米\n你的標準體重是"+weight+"公斤");
}

//四捨五入
private String format(double
num){
NumberFormat formatter = new DecimalFormat("0.00");
String s =
formatter.format(num);
return
s;
}

//以findViewById()取得Button對象,並添加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;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.