Android(安卓)Bundle类
16lz
2022-03-08
Android Bundle类
[日期:2011-08-11] | 来源:Linux社区 作者:randyjiawenjie |
发现自己连Bundle类都没有搞清楚,于是花时间研究了一下。
根据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>
跳转结果:
更多相关文章
- Android(安卓)中的单元测试(使用AndroidTestCase 进行 Content P
- WebKit – WebKit For Android
- Android中Message机制的灵活应用
- Android开发秘籍学习笔记(五)
- Android系统源码数据库(mmssms.db)
- android数据存储之File
- android开关飞行模式的方法
- mybatisplus的坑 insert标签insert into select无参数问题的解决
- python起点网月票榜字体反爬案例