Android教程:使用Bundle在Activity之间传递数据


Bundle可能过put****()方法添加各种类型的数据,Intent也可以通过putExtras(Bundle)将数据添加进去,然后通过startActivity()跳到下一下Activity的时候就把数据也传到下一个Activity了。

  1. package com.intent;  
  2.   
  3. import Android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class TestIntentActivity extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         Button button = (Button)this.findViewById(R.id.button);  
  17.         button.setOnClickListener(new OnClickListener() {  
  18.               
  19.             public void onClick(View v) {  
  20.                 Intent intent = new Intent(TestIntentActivity.this,SecondActivity.class);  
  21.                 Bundle bundle = new Bundle();  
  22.                 bundle.putString("key_name""name");  
  23.                 bundle.putString("key_age""age");  
  24.                 intent.putExtras(bundle);  
  25.                 startActivity(intent);  
  26.             }  
  27.         });  
  28.     }  
  29. }  

 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/intent"  
  11.         android:id="@+id/button" />  
  12.   
  13. </LinearLayout>  

 
  1. package com.intent;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6.   
  7. public class SecondActivity extends Activity{  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.second);  
  13.         TextView tv1 = (TextView)this.findViewById(R.id.tv1);  
  14.         TextView tv2 = (TextView)this.findViewById(R.id.tv2);  
  15.           
  16.         Bundle bundle = this.getIntent().getExtras();  
  17.         tv1.setText(bundle.getString("key_name"));  
  18.         tv2.setText(bundle.getString("key_age"));  
  19.     }  
  20.   
  21. }  
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:id="@+id/tv1"/>  
  10.     <TextView android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/tv2"/>  
  13. </LinearLayout>  

最后将新的Activity添加到manifest.xml里面就可以了

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容