延伸至  [Android] 不同Activity之間的資料傳遞 (Bundle)

本範例程式想要讓使用者先輸入資料,在下一畫面坐輸出的動作,先附上輸出畫面,以了解程式目的及介面結構。

EditText000  

EditText001  

EditText002  

Test01.java

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

public class Test01 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.layout0201);

    Button button = (Button)findViewById(R.id.button01);
    button.setOnClickListener(new Button.OnClickListener(){

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub

    EditText EditTextName = (EditText)findViewById(R.id.EditText01);
    Editable StrName;
    StrName = EditTextName.getText();
    String Name = StrName.toString();

    EditText EditTextTel = (EditText)findViewById(R.id.EditText02);
    Editable StrTel;
    StrTel = EditTextTel.getText();
    String Tel = StrTel.toString();

    EditText EditTextPhone = (EditText)findViewById(R.id.EditText03);
    Editable StrPhone;
    StrPhone = EditTextPhone.getText();
    String Phone = StrPhone.toString();

    EditText EditTextEmail = (EditText)findViewById(R.id.EditText04);
    Editable StrEmail;
    StrEmail = EditTextEmail.getText();
    String Email = StrEmail.toString();

    EditText EditTextName02 = (EditText)findViewById(R.id.EditText05);
    Editable StrName02;
    StrName02 = EditTextName02.getText();
    String Name02 = StrName02.toString();

    EditText EditTextTel02 = (EditText)findViewById(R.id.EditText06);
    Editable StrTel02;
    StrTel02 = EditTextTel02.getText();
    String Tel02 = StrTel02.toString();

    EditText EditTextPhone02 = (EditText)findViewById(R.id.EditText07);
    Editable StrPhone02;
    StrPhone02 = EditTextPhone02.getText();
    String Phone02 = StrPhone02.toString();

    EditText EditTextEmail02 = (EditText)findViewById(R.id.EditText08);
    Editable StrEmail02;
    StrEmail02 = EditTextEmail02.getText();
    String Email02 = StrEmail02.toString();

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

    //new一個Bundle物件,並將要傳遞的資料傳入
    Bundle bundle = new Bundle();
    bundle.putString("Name", Name);
    bundle.putString("Tel", Tel);
    bundle.putString("Phone", Phone);
    bundle.putString("Email", Email);
    bundle.putString("Name02", Name02);
    bundle.putString("Tel02", Tel02);
    bundle.putString("Phone02", Phone02);
    bundle.putString("Email02", Email02);
    //將Bundle物件assign給intent
    intent.putExtras(bundle);
    //切換Activity
    startActivity(intent);
    }
  });
 }
}

layout0201.xml  (Test01.java的layout)

<?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"
android:background="@color/white"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="一、緊急連絡人"
android:textSize="20sp"
android:textColor="@color/black"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 姓名 "
android:textSize="17sp"
android:textColor="@color/black"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditText01"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 電話 "
android:textSize="17sp"
android:textColor="@color/black"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditText02"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 手機 "
android:textSize="17sp"
android:textColor="@color/black"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditText03"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail "
android:textSize="17sp"
android:textColor="@color/black"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditText04"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="二、緊急連絡人"
android:textSize="20sp"
android:textColor="@color/black"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 姓名 "
android:textSize="17sp"
android:textColor="@color/black"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditText05"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 電話 "
android:textSize="17sp"
android:textColor="@color/black"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditText06"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 手機 "
android:textSize="17sp"
android:textColor="@color/black"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditText07"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail "
android:textSize="17sp"
android:textColor="@color/black"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditText08"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button01"
android:text="Save"
android:textSize="17sp"
/>
</LinearLayout>

Test02.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.TextView;

public class Test02 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.layout0202);

    Bundle bundle0311 =this.getIntent().getExtras();
    String Name = bundle0311.getString("Name");
    String Tel = bundle0311.getString("Tel");
    String Phone = bundle0311.getString("Phone");
    String Email = bundle0311.getString("Email");
    String Name02 = bundle0311.getString("Name02");
    String Tel02 = bundle0311.getString("Tel02");
    String Phone02 = bundle0311.getString("Phone02");
    String Email02 = bundle0311.getString("Email02");
    TextView tv1 = (TextView)findViewById(R.id.TextView01);
    TextView tv2 = (TextView)findViewById(R.id.TextView02);
    TextView tv3 = (TextView)findViewById(R.id.TextView03);
    TextView tv4 = (TextView)findViewById(R.id.TextView04);
    TextView tv5 = (TextView)findViewById(R.id.TextView05);
    TextView tv6 = (TextView)findViewById(R.id.TextView06);
    TextView tv7 = (TextView)findViewById(R.id.TextView07);
    TextView tv8 = (TextView)findViewById(R.id.TextView08);
    tv1.setText("姓名: "+Name);
    tv2.setText("電話: "+Tel);
    tv3.setText("手機: "+Phone);
    tv4.setText("E-mail: "+Email);
    tv5.setText("姓名: "+Name02);
    tv6.setText("電話: "+Tel02);
    tv7.setText("手機: "+Phone02);
    tv8.setText("E-mail: "+Email02);
  }
}

layout0202.xml (Test02.java的layout)

<?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"
android:orientation="vertical"
android:background="@color/white">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="緊急連絡人清單"
android:textSize="25sp"
android:textColor="@color/black"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="一、緊急連絡人"
android:textSize="20sp"
android:layout_marginTop="10sp"
android:textColor="@color/black"
/>
<TextView
android:layout_marginTop="10sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:id="@+id/TextView01"
android:layout_marginLeft="10sp"
android:textColor="@color/black"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:id="@+id/TextView02"
android:layout_marginLeft="10sp"
android:textColor="@color/black"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:id="@+id/TextView03"
android:layout_marginLeft="10sp"
android:textColor="@color/black"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:id="@+id/TextView04"
android:layout_marginLeft="10sp"
android:textColor="@color/black"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="二、緊急連絡人"
android:textSize="20sp"
android:layout_marginTop="10sp"
android:textColor="@color/black"
/>
<TextView
android:layout_marginTop="10sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:id="@+id/TextView05"
android:layout_marginLeft="10sp"
android:textColor="@color/black"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:id="@+id/TextView06"
android:layout_marginLeft="10sp"
android:textColor="@color/black"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:id="@+id/TextView07"
android:layout_marginLeft="10sp"
android:textColor="@color/black"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:id="@+id/TextView08"
android:layout_marginLeft="10sp"
android:textColor="@color/black"
/>
</LinearLayout>

arrow
arrow
    創作者介紹
    創作者 S 的頭像
    S

    S's Journal

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