Android Bundle类
Android Bundle类
[日期:2011-08-11] | 来源:Linux社区 作者:randyjiawenjie |
发现自己连Bundle类都没有搞清楚,于是花时间研究了一下。
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)
(3)新建一个intent对象,并将该bundle加入这个intent对象
完整代码如下:
两个类如下:intent从TestBundle类发起,到Target类。
类2: Target
布局文件:
结果:
根据google官方的文档(http://developer.Android.com/reference/android/os/Bundle.html)
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类
- BundlemBundle=newBundle();
- mBundle.putString("Data","datafromTestBundle");
- Intentintent=newIntent();
- intent.setClass(TestBundle.this,Target.class);
- intent.putExtras(mBundle);
Android mainfest.xml如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:Android="http://schemas.android.com/apk/res/android"
- package="com.tencent.test"
- Android:versionCode="1"
- Android:versionName="1.0">
- <applicationAndroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityAndroid:name=".TestBundle"
- Android:label="@string/app_name">
- <intent-filter>
- <actionAndroid:name="android.intent.action.MAIN"/>
- <categoryAndroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <activityAndroid:name=".Target"></activity>
- </application>
- <uses-sdkAndroid:minSdkVersion="7"/>
- </manifest>
类1:TestBundle类:
- importAndroid.app.Activity;
- importAndroid.content.Intent;
- importAndroid.os.Bundle;
- importAndroid.view.View;
- importAndroid.view.View.OnClickListener;
- importAndroid.widget.Button;
- publicclassTestBundleextendsActivity{
- privateButtonbutton1;
- privateOnClickListenercl;
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button1=(Button)findViewById(R.id.button1);
- cl=newOnClickListener(){
- @Override
- publicvoidonClick(Viewarg0){
- //TODOAuto-generatedmethodstub
- Intentintent=newIntent();
- intent.setClass(TestBundle.this,Target.class);
- BundlemBundle=newBundle();
- mBundle.putString("Data","datafromTestBundle");//压入数据
- intent.putExtras(mBundle);
- startActivity(intent);
- }
- };
- button1.setOnClickListener(cl);
- }
- }
- importAndroid.app.Activity;
- importAndroid.os.Bundle;
- publicclassTargetextendsActivity{
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.target);
- <spanstyle="color:#ff6600;">Bundlebundle=getIntent().getExtras();</span>//得到传过来的bundle
- Stringdata=bundle.getString("Data");//读出数据
- setTitle(data);
- }
- }
main.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:Android="http://schemas.android.com/apk/res/android"
- Android:orientation="vertical"
- Android:layout_width="fill_parent"
- Android:layout_height="fill_parent"
- >
- <TextView
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:text="@string/hello"
- />
- <Button
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:text="@string/button"
- Android:id="@+id/button1"
- />
- </LinearLayout>
target.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:Android="http://schemas.android.com/apk/res/android"
- Android:orientation="vertical"
- Android:layout_width="fill_parent"
- Android:layout_height="fill_parent"
- >
- <TextView
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:text="@string/target"
- />
- </LinearLayout>
String.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <stringname="hello">HelloWorld,TestBundle!</string>
- <stringname="app_name">测试Bundle用法</string>
- <stringname="button">点击跳转</string>
- <stringname="target">来到targetactivity</string>
- </resources>
跳转结果: