利用Bundle来封装数据,然后用intent传递
来源:互联网 发布:买哪个星空软件 编辑:程序博客网 时间:2024/04/20 03:25
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 = 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); } }
在第一个的布局文件中添加一个按钮
第二个布局文件中可随便输出一些文本
0 0
- 利用Bundle来封装数据,然后用intent传递
- 数据传递intent,Bundle
- android intent 用bundle传递数据
- 利用Intent技术实现Activity之间传递Bundle类型数据
- Android Intent传递Bundle数据
- Intent和Bundle传递数据
- Intent 传递数据,bundle 传递数组
- Intent 传递数据,bundle 传递数组
- Intent 传递数据,bundle 传递数组
- Intent 传递数据,bundle 传递数组
- 使用Intent 利用bundle传递对象
- 利用Intent传递数据
- Handler利用bundle传递数据
- android开发 -- 利用intent来传递int数据
- 使用Intent和Bundle的数据传递
- Android中Intent结合Bundle传递数据
- [Android] Intent及Bundle传递数据
- Android使用intent、bundle传递数据
- 设计模式之-----命令模式
- UITableView
- LeetCode(151) Reverse Words in a String
- 单选框、多选框的在线填写写法
- DSP28335硬件教程-xintf接口
- 利用Bundle来封装数据,然后用intent传递
- ubuntu下sts不显示菜单
- 借助motion操控Linux下的摄像头
- 函数返回局部变量
- ACE_NEW开头的宏定义
- UGUI 扩展集
- sysbench
- 基于Servlet、JSP、JDBC、MySQL的一个简单的用户注册模块(附完整源码)
- 浅谈网站内容建设、数据分析、信息架构三者关系