Android Bundle类

2023-11-11

今天发现自己连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类

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如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tencent.test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TestBundle"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
	<activity android:name=".Target"></activity>
    </application>
    <uses-sdk android:minSdkVersion="7" />
</manifest> 

两个类如下:intent从TestBundle类发起,到Target类。

类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);  
        Bundle bundle = getIntent().getExtras();    //得到传过来的bundle
        String data = bundle.getString("Data");//读出数据  
        setTitle(data);  

    }  
}  

布局文件:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, TestBundle!</string>
    <string name="app_name">测试Bundle用法</string>
    <string name="button">点击跳转</string>
    <string name="target">来到target activity</string>
</resources>

结果:


跳转结果:


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Bundle类 的相关文章

随机推荐

  • 15_插入排序算法(附java代码)

    15 插入排序算法 一 基本介绍 插入式排序属于内部排序法 是对于欲排序的元素以插入的方式找寻该元素的适当位置 以达到排序的目的 二 插入排序算法 2 1 算法思想 插入排序 Insertion Sorting 的基本思想是 把n个待排序的
  • 每天五分钟机器学习:使用支持向量机的时候,如何选择模型参数?

    本文重点 上一章 我们学习了支持向量机的特征高维映射 也就是如何将特征映射到高维 在支持向量机中有两个参数需要调节 第一个参数是C 第二个参数是 C C 1 入 因此 C较大时 相当于 较小 意味着不使用正则化 可能会导致过拟合 高方差 C
  • kylin启动:Failed to create /kylin

    文章目录 一 当在ubuntu下执行check env sh时遇到问题 KYLIN HOME is set to usr local apache kylin 1 5 4 1 bin cat invalid option 1 Try cat
  • 用最少的线段覆盖点

    假设现有一平面网格 上有N个点 现用直线段把每个点都覆盖住 线段不能折 只能水平或者竖直方向 如何求出使用的线段最少 如下图所示 上面一个网格 有7个点 其中用三条线段就能覆盖住所有点 下图8个点 也是同样三条线段覆盖住8个点
  • 嵌入式学习笔记--GEC6818--bmp图片显示

    一 开发环境 1 ARM linux 2 ubuntu16 04 安装了GCC arm linux gcc 5 4 0 安装方法ubuntu16 04https blog csdn net qq 40592257 article detai
  • Ubuntu下FastDFS的安装

    准备工作 刚安装好的linux系统没有设置root用户密码的 下边介绍如何设置root用户的密码 由于ubtun系统默认是没有激活root用户的 需要我们手工进行操作 在命令行界面下 或者在终端中输入如下命令 sudo passwd 或者s
  • 删除数字问题

    已知n位数字正整数a 去除任意k位数 使剩下数字按原次序排列成新正整数 使其最大 C语言 include
  • 最全地理数据下载网址

    晓码创作 整理不易 转载引用请写明出处 一 国内 常见遥感影像下载网址 https mp weixin qq com s xzNGmz1fgb3IqbOV EN1Aw 1 http data cma cn site index html 中
  • Modbus驱动库—libmodbus驱动库的使用

    文章目录 为什么要使用驱动库 libmodbus简介 libmodbus常用函数 Windows平台libmodbus 使用 1 获取源代码 2 生成config h配置文件 3 编写测试代码 4 编译测试代码 Linux平台下libmod
  • C语言循环链表实现约瑟夫环问题

    问题描述 约瑟夫环问题 1 问题描述 设有编号为1 2 n的n n 0 个人围成一个圈 每个人持有一个密码m 从第一个人开始报数 报到m时停止报数 报m的人出圈 再从他的下一个人起重新报数 报到m时停止报数 报m的出圈 如此下去 直到所有人
  • 轻松入门自然语言处理系列 14 Linear-CRF模型

    文章目录 前言 一 Log Linear模型与逻辑回归 1 Log Linear模型 2 从Log Linear模型到逻辑回归 二 Linear CRF模型Inference 1 Linear CRF模型定义 2 Linear CRF模型I
  • 2021年安全员-A证免费试题及安全员-A证考试技巧

    题库来源 安全生产模拟考试一点通公众号小程序 安全员 A证免费试题考前必练 安全生产模拟考试一点通每个月更新安全员 A证考试技巧题目及答案 多做几遍 其实通过安全员 A证很简单 1 判断题 生产经营规模较小的 可以不建立应急救援组织 但应当
  • Each module has to have a unique path

    在android工作中从仓库中down下来的时候 首次运行出现 Each module has to have a unique path 使用android studio开发工具 因为同事一起把 iml文件给上传上去了 每次builde都
  • RISC-V MCU开发 (七):代码调试

    RISC V MCU开发 七 代码调试 MounRiver Studio MRS 配合GD Link WCH Link JLink等在线调试下载器 可支持GDVFx系列 CH56x CH57x CH58x CH32Fx CH32Vx等RIS
  • ‘入门级’程序员面试关键字眼解答

    JSP 全名为 Java Server Pages 中文名叫java服务器页面 其根本是一个简化的Servlet 设计 它是在传统的网页HTML文件 中插入Java程序段 Scriptlet 和JSP标记 tag 从而形成JSP文件 后缀名
  • LaTeX/Markdown中如何在字母上下方插入字母数字

    1 一般的写法 P Y c k X x frac P Y c k prod j P X j x j Y c k sum k P Y c k prod j P X j x j Y c k 效果展示为 P Y
  • 【QT学习教程2】文件操作功能实现

    目录 0 前言 一 界面布局 二 文件操作功能的实现 2 1 思路整理 2 2 初始化界面功能的实现 2 3 判断当前文本是否可以 修改 的功能实现 2 4 新建文本功能的实现 2 5 保存功能的实现 2 6 另存为功能的实现 2 7 文件
  • 使用多线程或异步技术提高图片抓取效率

    导语 图片抓取是爬虫技术中常见的需求 但是图片抓取的效率受到很多因素的影响 比如网速 网站反爬机制 图片数量和大小等 本文将介绍如何使用多线程或异步技术来提高图片抓取的效率 以及如何使用爬虫代理IP来避免被网站封禁 概述 多线程和异步技术都
  • STM32与物联网02-网络数据收发

    优质资源分享 学习路线指引 点击解锁 知识定位 人群定位 Python实战微信订餐小程序 进阶级 本课程是python flask 微信小程序的完美结合 从项目搭建到腾讯云部署上线 打造一个全栈订餐系统 Python量化交易实战 入门级 手
  • Android Bundle类

    今天发现自己连Bundle类都没有搞清楚 于是花时间研究了一下 根据google官方的文档 http developer android com reference android os Bundle html Bundle类是一个key