android bundle 作用,Android中Bundle類的作用

Android中Bundle類的作用

Bundle類用作攜帶數據,它類似於Map,用於存放key-value名值對形式的值

今天發現自己連Bundle類都沒有搞清楚,於是花時間研究了一下。

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類

Bundle mBundle =newBundle();

(2)bundle類中加入數據(key -value的形式,另一個activity里面取數據的時候,就要用到key,找出對應的value)

mBundle.putString("Data","data from TestBundle");

(3)新建一個intent對象,並將該bundle加入這個intent對象

Intent intent =newIntent();

intent.setClass(TestBundle.this, Target.class);

intent.putExtras(mBundle);

完整代碼如下:

android mainfest.xml如下:

package="com.tencent.test"

android:versionCode="1"

android:versionName="1.0">

android:label="@string/app_name">

兩個類如下:intent從TestBundle類發起,到Target類。

類1:TestBundle類:

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

publicclassTestBundleextendsActivity {

privateButton button1;

privateOnClickListener cl;

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

button1 = (Button) findViewById(R.id.button1);

cl = newOnClickListener(){

@Override

publicvoidonClick(View arg0) {

// TODO Auto-generated method stub

Intent intent = newIntent();

intent.setClass(TestBundle.this, Target.class);

Bundle mBundle = newBundle();

mBundle.putString("Data","data from TestBundle");//壓入數據

intent.putExtras(mBundle);

startActivity(intent);

}

};

button1.setOnClickListener(cl);

}

}

類2: Target

importandroid.app.Activity;

importandroid.os.Bundle;

publicclassTargetextendsActivity{

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.target);

Bundle bundle = getIntent().getExtras();   //得到傳過來的bundle

String data = bundle.getString("Data");//讀出數據

setTitle(data);

}

}

布局文件:

main.xml

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/button"

android:id = "@+id/button1"

/>

target.xml

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/target"

/>

String.xml

Hello World, TestBundle!

測試Bundle用法

點擊跳轉

來到target activity

結果:

aHR0cDovL2hpLmNzZG4ubmV0L2F0dGFjaG1lbnQvMjAxMTA4LzEvOTI3NzU1MV8xMzEyMTkwMTY2bHFOTi5qcGc=

跳轉結果:

aHR0cDovL2hpLmNzZG4ubmV0L2F0dGFjaG1lbnQvMjAxMTA4LzEvOTI3NzU1MV8xMzEyMTkwMTY2VWE3Ny5qcGc=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值