Android應用開發學習筆記之使用Bundle交換資料

來源:互聯網
上載者:User

首先看主布局檔案main.xml:

[html]
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" > 
 
    <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="http://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="http://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="http://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> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.