Android中Bundle的使用

来源:互联网 发布:黑暗的中世纪 知乎 编辑:程序博客网 时间:2024/05/29 07:09

Bundle类是一个key-value对,“A mapping from String values to various Parcelable types.

类继承关系:

java.lang.Object
     Android.os.Bundle
Bundle类是一个final类:
public final class
Bundle
extends Objectimplements Parcelable Cloneable

两个activity之间的通讯可以通过bundle类来实现,做法就是:

(1)新建一个bundle类

[java] view plain copy
  1. Bundle mBundle = new Bundle();   
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)
[java] view plain copy
  1. mBundle.putString("Data""data from TestBundle");  
(3)新建一个intent对象,并将该bundle加入这个intent对象
[cpp] view plain copy
  1. Intent intent = new Intent();    
  2. intent.setClass(TestBundle.this, Target.class);    
  3. intent.putExtras(mBundle);  
两个类如下:intent从TestBundle类发起,到Target类。

类1:TestBundle类:

[java] view plain copy
  1. import android.app.Activity;    
  2. import android.content.Intent;    
  3. import android.os.Bundle;    
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7.   
  8. public class TestBundle extends Activity {    
  9.       
  10.     private Button button1;  
  11.     private OnClickListener cl;   
  12.     public void onCreate(Bundle savedInstanceState) {    
  13.         super.onCreate(savedInstanceState);    
  14.         setContentView(R.layout.main);  
  15.           
  16.         button1 = (Button) findViewById(R.id.button1);  
  17.         cl = new OnClickListener(){  
  18.             @Override  
  19.             public void onClick(View arg0) {  
  20.                 // TODO Auto-generated method stub  
  21.                 Intent intent = new Intent();    
  22.                 intent.setClass(TestBundle.this, Target.class);    
  23.                 Bundle mBundle = new Bundle();    
  24.                 mBundle.putString("Data""data from TestBundle");//压入数据    
  25.                 intent.putExtras(mBundle);    
  26.                 startActivity(intent);  
  27.             }  
  28.         };  
  29.         button1.setOnClickListener(cl);  
  30.     }  
  31. }    

类2: Target

[java] view plain copy
  1. import android.app.Activity;    
  2. import android.os.Bundle;    
  3.   
  4. public class Target extends Activity{    
  5.   
  6.     public void onCreate(Bundle savedInstanceState) {    
  7.           
  8.         super.onCreate(savedInstanceState);    
  9.         setContentView(R.layout.target);    
  10.         <span style="color:#ff6600;">
  11.         Bundle bundle = getIntent().getExtras();   
  12.         </span> //得到传过来的bundle  
  13.         String data = bundle.getString("Data");//读出数据    
  14.         setTitle(data);    
  15.   
  16.     }    
  17. }    


概况为以下几步:

1、实例化Bundle 一个对象,用putString(标记,数据)来将数据导入到Bundle对象中;  
2、然后将Bundle对象导入到Intent对象中;
3、Intent启动另一个activity。
4、Bundle对象可以从activity.getIntent().getExtras()中返回。可见,启动当前activity 
      的Intent对象是由getIntent()来找到的。
5、通过Bundle的getString()方法,就可以读出所要的数据。






1 0
原创粉丝点击