除了Activity之間的切換之外,常常需要在呼叫另一個Activity的同時傳遞資料,所以就必須要利用android.os.Bundle物件封裝資料的能力,將需傳遞的資料或參數,藉由Bundle來傳遞於不同Intent之間。

小試身手小範例

本範例為一個簡易的標準體重計算器,首先在Activity1中收集User輸入的資料,在離開Activity1的同時將User填寫的資料傳遞到下一個Activity2計算並輸出。

bundle01jpg.jpg

bundle02.jpg

example0310.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class example0310 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);

        Button CalculateButton = (Button)findViewById(R.id.Button01);
        CalculateButton.setOnClickListener(new Button.OnClickListener(){

              @Override
              public void onClick(View v) {
              // TODO Auto-generated method stub
              EditText HeightEditText = (EditText)findViewById(R.id.EditText01);
              double height = Double.parseDouble(HeightEditText.getText().toString());

              String sex="";
              RadioButton SexMale = (RadioButton)findViewById(R.id.RadioButton01);
              if(SexMale.isChecked()){
              sex="M";
              }
              else{
              sex="F";
              }

              //new一個intent物件,並指定Activity切換的class
              Intent intent = new Intent();
              intent.setClass(example0310.this, example0311.class)

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

              //將Bundle物件assign給intent
              intent.putExtras(bundle);

              //切換Activity
              startActivity(intent);
              }

        });
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="標準體重計算器"
android:textSize="22sp"
/>

<TextView
android:layout_marginTop="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性別"
android:textSize="20sp"
android:id="@+id/TextView02"
/>
<RadioGroup
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/RadioGroup01"
>
<RadioButton
android:text="男"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/RadioButton01"
android:checked="true"
/>
<RadioButton
android:text="女"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/RadioButton02"
/>
</RadioGroup>
<TextView
android:layout_marginTop="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高"
android:textSize="20sp"
android:id="@+id/TextView02"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:layout_width="150sp"
android:layout_height="wrap_content"
android:id="@+id/EditText01"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" cm"
android:textSize="20sp"
/>
</LinearLayout>
<Button
android:layout_marginTop="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="計算"
android:textSize="17sp"
android:id="@+id/Button01"
/>
</LinearLayout>

example0311.java

import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class example0311 extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);
       requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
       WindowManager.LayoutParams.FLAG_FULLSCREEN);
       setContentView(R.layout.finallayout);

       //取的intent中的bundle物件
       Bundle bundle0311 =this.getIntent().getExtras();

       String sex = bundle0311.getString("sex");
       double height = bundle0311.getDouble("height");

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

       //取得標準體中
       String weight = this.getWeight(sex,height);
       //設定輸出
       TextView tv1 = (TextView)findViewById(R.id.TextView11);
       tv1.setText("您是一位"+sexText+"\n您的身高是"+height+"cm\n您的標準體重是"+weight+"kg");
       }

       private String getWeight(String sex, double height) {
       // TODO Auto-generated method stub
       String weight="";
       if(sex.equals("M")){
       weight=format((height-80)*0.7);
       }else{
       weight=format((height-70)*0.6);
       }
       return weight;
}

//四捨五入用
private String format(double num) {
// TODO Auto-generated method stub
      NumberFormat formatter = new DecimalFormat("0.00");
      String s = formatter.format(num);
      return s;
    }

}

finallayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:layout_marginTop="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/TextView11"
/>
</LinearLayout>

 

延伸閱讀  [Android] EditText內String資料傳到下一個Activity (Bundle)

 

  

  


arrow
arrow

    S 發表在 痞客邦 留言(5) 人氣()