Android應用開發學習筆記之使用Bundle交換數據

首先看主佈局文件main.xml:

[html]
xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="請輸入第一個數字:" /> 
    <EditText  
        android:id="@+id/editText1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />" 
     
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="請輸入第二個數字:" /> 
    <EditText  
        android:id="@+id/editText2" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 
     
    <Button  
        android:id="@+id/button" 
        android:text="提交" 
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" />" 
     
</LinearLayout> 

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="請輸入第一個數字:" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />"
   
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="請輸入第二個數字:" />
    <EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
   
    <Button
        android:id="@+id/button"
        android:text="提交"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />"
   
</LinearLayout>
下面來看主Activity文件,其內容如下:

[java]
package com.liuhaoyu; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
 
public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        final EditText edit1 = (EditText)findViewById(R.id.editText1); 
        final EditText edit2 = (EditText)findViewById(R.id.editText2); 
         
        Button button = (Button)findViewById(R.id.button); 
        button.setOnClickListener(new View.OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub  
                String num1 = edit1.getText().toString(); 
                String num2 = edit2.getText().toString(); 
                Intent intent=new Intent(MainActivity.this, SecondActivity.class); 
                Bundle bundle = new Bundle(); 
                bundle.putCharSequence("number1", num1); 
                bundle.putCharSequence("number2", num2); 
                intent.putExtras(bundle); 
                startActivity(intent); 
            } 
        }); 
    } 

package com.liuhaoyu;

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

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        final EditText edit1 = (EditText)findViewById(R.id.editText1);
        final EditText edit2 = (EditText)findViewById(R.id.editText2);
       
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    String num1 = edit1.getText().toString();
    String num2 = edit2.getText().toString();
    Intent intent=new Intent(MainActivity.this, SecondActivity.class);
    Bundle bundle = new Bundle();
    bundle.putCharSequence("number1", num1);
    bundle.putCharSequence("number2", num2);
    intent.putExtras(bundle);
    startActivity(intent);
   }
  });
    }
}
在按鈕的監聽函數中,創建瞭一個Bundle對象,將用戶輸入的數值保存在其中,然後該Bundle做為Intent的附加參數傳遞給SecondActivity。

下面看SecondActivity的實現,其內容如下:

[java]
package com.liuhaoyu; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class SecondActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.second); 
        int sum; 
        Intent intent = getIntent(); 
        Bundle bundle = intent.getExtras(); 
        String num1 = bundle.getString("number1"); 
        String num2 = bundle.getString("number2"); 
         
        sum = Integer.parseInt(num1) + Integer.parseInt(num2); 
         
        TextView text = (TextView)findViewById(R.id.textView); 
        text.setText(num1 + " + " + num2 + " = " + String.valueOf(sum)); 
    } 

package com.liuhaoyu;

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

public class SecondActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        int sum;
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String num1 = bundle.getString("number1");
        String num2 = bundle.getString("number2");
       
        sum = Integer.parseInt(num1) + Integer.parseInt(num2);
       
        TextView text = (TextView)findViewById(R.id.textView);
        text.setText(num1 + " + " + num2 + " = " + String.valueOf(sum));
    }
}
在onCreate函數中,首先通過getIntent()取得Intent,再通過Intent.getExtras()取得MainActivity傳遞過來的Bundle,由此即可以獲得傳遞過來的數值瞭。

SecondActivity的佈局文件內容如下:

[html]
xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
     
    <TextView  
        android:id="@+id/textView" 
        android:textSize="20dp" 
        android:layout_gravity="center" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />" 
 
</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
    <TextView
        android:id="@+id/textView"
        android:textSize="20dp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />"

</LinearLayout>
最後,在AndroidManifest.xml文件中加入SecondActivity的聲明:

[html]
         <activity  
     android:icon="@drawable/ic_launcher" 
     android:name=".SecondActivity" 
     android:label="計算結果" 
     android:theme="@android:style/Theme.Dialog" 
     > 
</activity> 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *