利用Bundle来封装数据,然后用intent传递
2015-08-10 09:05
309 查看
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
在第一个的布局文件中添加一个按钮
第二个布局文件中可随便输出一些文本
类继承关系:
java.lang.Object
android.os.Bundle
Bundle类是一个final类:
public final class
Bundle
extends Objectimplements Parcelable Cloneable
两个activity之间的通讯可以通过bundle类来实现,做法就是:
(1)新建一个bundle类 Bundle mBundle = new Bundle(); (2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value) mBundle.putString("Data", "data from TestBundle"); (3)新建一个intent对象,并将该bundle加入这个intent对象 Intent intent = new Intent(); intent.setClass(TestBundle.this, Target.class); intent.putExtras(mBundle);
android mainfest.xml如下:
添加<activity android:name=".Target"></activity>
类1:TestBundle类:
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestBundle extends Activity { private Button button1; private OnClickListener cl; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button) findViewById(R.id.button1); cl = new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(TestBundle.this, Target.class); Bundle mBundle = new Bundle(); mBundle.putString("Data", "data from TestBundle");//压入数据 intent.putExtras(mBundle); startActivity(intent); } }; button1.setOnClickListener(cl); } }
类2: Target
import android.app.Activity; import android.os.Bundle; public class Target extends Activity{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.target); <span style="color:#ff6600;">Bundle bundle = getIntent().getExtras(); </span> //得到传过来的bundle String data = bundle.getString("Data");//读出数据 setTitle(data); } }
在第一个的布局文件中添加一个按钮
第二个布局文件中可随便输出一些文本
相关文章推荐
- [CareerCup] 5.1 Insert Bits 插入位
- 3.2Library string Type
- hdoj1224Free DIY Tour【dp】
- An Interpretation of Logistic Regression
- UITableView
- jquery ui dialog autofocus 去掉默认第一个元素获取焦点
- opencv下摄像头的采集和保存
- 【Spring】Eclipse导入Spring包生成SpringMVC项目的注意事项
- POJ3468 A Simple Problem with Integers(线段树)
- Python于*args 和**kwargs使用
- Introduction to H2 Database
- Android-database
- Stockbroker Grapevine【POJ--1125】【Floyd】
- POJ 3267 The Cow Lexicon(DP)
- SqlServer表EXCEL数据复制的另一种方法
- Flask的部署
- OWIN概述
- UI08_TableView界面传值
- 欢迎使用CSDN-markdown编辑器
- 如何轻松实现iOS9多任务管理器效果(iCarousel高级教程)