Android教程(十四)– Activity間的數據傳輸

在上一章我們說到瞭Activity之間的跳轉,在本章我們來說說數據在它們之間是如何進行傳輸的。

數據傳輸所用到的載體是Bundle,好的我們直接就進入主題瞭。

第一步,建立一個工程Ep.Bundle。

第二步,設計一下視圖,我的視圖是這樣的:

activity_main.xml

[java] 
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 
    xmlns:tools="https://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".Main2Activity" > 
 
    <TextView 
        android:id="@+id/textView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="請輸入你的信息:" /> 
 
    <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/textView2" 
        android:layout_below="@+id/textView2" 
        android:layout_marginTop="15dp" 
        android:text="名字:" /> 
 
    <TextView 
        android:id="@+id/textView3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/textView1" 
        android:layout_below="@+id/textView1" 
        android:layout_marginTop="16dp" 
        android:text="性別:" /> 
 
    <EditText 
        android:id="@+id/editText1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignBaseline="@+id/textView1" 
        android:layout_alignBottom="@+id/textView1" 
        android:layout_centerHorizontal="true" 
        android:ems="10" 
        android:inputType="date" > 
 
        <requestFocus /> 
    </EditText> 
 
    <RadioGroup 
        android:id="@+id/radioGroup1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/editText1" 
        android:layout_alignTop="@+id/textView3" 
        android:orientation="horizontal" > 
 
        <RadioButton 
            android:id="@+id/radio0" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:checked="true" 
            android:text="男" /> 
 
        <RadioButton 
            android:id="@+id/radio1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="女" /> 
    </RadioGroup> 
 
    <Button 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/radioGroup1" 
        android:layout_marginTop="34dp" 
        android:layout_toRightOf="@+id/textView2" 
        android:text="提交" /> 
 
</RelativeLayout> 

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main2Activity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="請輸入你的信息:" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="15dp"
        android:text="名字:" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp"
        android:text="性別:" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="date" >

        <requestFocus />
    </EditText>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignTop="@+id/textView3"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginTop="34dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="提交" />

</RelativeLayout>
activity_main2.xml

 

[java] 
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 
    xmlns:tools="https://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".Main2Activity" > 
 
    <TextView 
        android:id="@+id/textView4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="請輸入你的信息:" /> 
 
     
 
</RelativeLayout> 

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main2Activity" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="請輸入你的信息:" />

   

</RelativeLayout>
第三步,寫核心文件。

MainActivity.java

[java] 
package com.example.ep2.bundle; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RadioButton; 
 
public class MainActivity extends Activity { 
 
    private Button bt; 
    private EditText et; 
    private RadioButton rd; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         
        bt=(Button)findViewById(R.id.button1); 
        et=(EditText)findViewById(R.id.editText1); 
        rd=(RadioButton)findViewById(R.id.radio0); 
         
        bt.setOnClickListener(new OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub  
                String name=et.getText().toString().trim(); 
                String sex=""; 
                if(rd.isChecked()){ 
                    sex="Man"; 
                }else{ 
                    sex="Women"; 
                } 
                Intent it=new Intent(); 
                it.setClass(MainActivity.this, Main2Activity.class); 
                Bundle bd=new Bundle(); 
                bd.putString("name", name); 
                bd.putString("sex", sex); 
                it.putExtras(bd); 
                startActivity(it); 
            } 
        }); 
    } 
 

package com.example.ep2.bundle;

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

public class MainActivity extends Activity {

 private Button bt;
 private EditText et;
 private RadioButton rd;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  bt=(Button)findViewById(R.id.button1);
  et=(EditText)findViewById(R.id.editText1);
  rd=(RadioButton)findViewById(R.id.radio0);
  
  bt.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    String name=et.getText().toString().trim();
    String sex="";
    if(rd.isChecked()){
     sex="Man";
    }else{
     sex="Women";
    }
    Intent it=new Intent();
    it.setClass(MainActivity.this, Main2Activity.class);
    Bundle bd=new Bundle();
    bd.putString("name", name);
    bd.putString("sex", sex);
    it.putExtras(bd);
    startActivity(it);
   }
  });
 }

}

Main2Activity.java

[java] 
package com.example.ep2.bundle; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.widget.TextView; 
 
public class Main2Activity extends Activity { 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main2); 
         
        Bundle bd=this.getIntent().getExtras(); 
        String name= bd.getString("name"); 
        String sex=bd.getString("sex"); 
         
        TextView tv = (TextView)findViewById(R.id.textView4); 
        tv.setText("名字:"+name+"/n 性別:"+sex); 
         
    } 

package com.example.ep2.bundle;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class Main2Activity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main2);
  
  Bundle bd=this.getIntent().getExtras();
  String name= bd.getString("name");
  String sex=bd.getString("sex");
  
  TextView tv = (TextView)findViewById(R.id.textView4);
  tv.setText("名字:"+name+"/n 性別:"+sex);
  
 }
}

 

You May Also Like