Android Bundle类别

即使在今天发现自己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(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent 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();    //得到传过来的bundleString 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>


结果:


跳转结果:



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544687.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C# Winform 窗体美化(七、Win7 Aero 毛玻璃效果)

七、Win7 Aero 毛玻璃效果 在 Win7 上有一种 Aero 效果&#xff0c;毛玻璃透明效果&#xff0c;搭配不同风格的颜色&#xff0c;效果很好。在学习 Winform 美化的时候顺便看到的这种效果&#xff0c;也整理进来了。 注意&#xff1a;Win7 上想看到这种效果需要开启并使用 Aer…

非导向传输媒体| 计算机网络

Transmission media can be categorized in two ways, 传输媒体可以通过两种方式进行分类&#xff1a; Guided Transmission Media 引导传输媒体 Unguided Transmission Media 非引导传输媒体 1)非引导传输媒体 (1) Unguided Transmission Media) It is also called wireless …

面霸篇:MQ 的 5 大关键问题详解

最近mq越来越火&#xff0c;很多公司在用&#xff0c;很多人在用&#xff0c;其重要性不言而喻。但是如果我让你回答下面的这些问题&#xff1a;我们为什么要用mq&#xff1f;引入mq会多哪些问题&#xff1f;如何解决这些问题&#xff1f;你心中是否有答案了呢&#xff1f;本文…

C# Winform 窗体美化(八、Icon)

八、Icon 之前 Winform 项目也有在 Icon 上遇到些问题&#xff08;这里的 Icon 指的是 .ico 类型的文件&#xff09;&#xff0c;比如刚开始不知道怎么让自己的程序 Icon 和其他软件一样可以放大&#xff0c;还有放大之后在音量合成器中会出现比较奇葩的效果之类的问题&#x…

Java LocalDate类| 带示例的getEra()方法

LocalDate类的getEra()方法 (LocalDate Class getEra() method) getEra() method is available in java.time package. getEra()方法在java.time包中可用。 getEra() method is used to get the era applicable for this LocalDate object. getEra()方法用于获取适用于此LocalD…

MyBatis 的执行流程,学废了!

作者&#xff1a;双子孤狼来源&#xff1a;blog.csdn.net/zwx900102/article/details/108455514MyBatis可能很多人都一直在用&#xff0c;但是MyBatis的SQL执行流程可能并不是所有人都清楚了&#xff0c;那么既然进来了&#xff0c;通读本文你将收获如下&#xff1a;1、Mapper接…

C# Winform 窗体美化(九、嵌入窗体)

九、嵌入窗体 还是关于 Winform 窗体的一些操作问题&#xff0c;这次是研究了一个嵌入窗体&#xff0c;这次学习纯属偶然&#xff0c;项目中确实没遇到过这种需求。就是把别人的程序嵌入到自己的程序中&#xff0c;就像这样&#xff1a; 这里我嵌入了测试显示器的程序 [外链图…

Mac 流程图

https://www.lucidchart.com/pages/signup?utm_expid39895073-174.qKyHpBEbQS26y86OArD-rQ.1 https://www.processon.com/

setname_Python线程类| setName()方法与示例

setnamePython Thread.setName()方法 (Python Thread.setName() Method) Thread.setName() method is an inbuilt method of the Thread class of the threading module in Python. It uses a Thread object and sets the name of the thread. Thread.setName()方法是Python中线…

SpringBoot 优雅的参数效验!

引言 不知道大家平时的业务开发过程中 controller 层的参数校验都是怎么写的&#xff1f;是否也存在下面这样的直接判断&#xff1f;public String add(UserVO userVO) {if(userVO.getAge() null){return "年龄不能为空";}if(userVO.getAge() > 120){return &quo…

NTFS Change Journal(USN Journal)详解

写在前面 最近又用了一下usn日志来获取所有文件列表&#xff0c;在分多次加载文件列表的时候发现有文件丢失的情况&#xff0c;后来发现一篇文章比较详细的讲了usn。 用cmd来读取usn日志 如图&#xff1a; 以下是转载内容&#xff1a; 还是那个文件监控的应用&#xff0c;…

Java即时类| hashCode()方法与示例

即时类hashCode()方法 (Instant Class hashCode() method) hashCode() method is available in java.time package. hashCode()方法在java.time包中可用。 hashCode() method is used to get the hash code value for this Instant. hashCode()方法用于获取此Instant的哈希码值…

系统起动时加载的过程

sof_getdjval转载于:https://blog.51cto.com/bks2015/1660178

绝,Java 中创建对象的 5 种方法!

我们日常生活中会创建很多对象&#xff0c;但是这个对象和你理解的那么对象不一样&#xff0c;因为作者不是女娲&#xff0c;不能造人。作者只是程序员&#xff0c;他只能在 Java 中创建对象。那么我问你一个问题&#xff0c;你知道 Java 中如何创建对象吗&#xff1f;这个问题…

C# Winform 窗体美化(十、自定义窗体)

十、自定义窗体 写在前面 最近在做 winform 应用程序&#xff0c;需要自定义一种窗口的样式&#xff0c;所以就随便搞了一个简单的窗口。 效果图 有两种样式&#xff0c;界面如下&#xff1a; 无标题&#xff1a; 有标题&#xff1a; 关键词 1、黑色描边边框 对于…

几种在shell命令行中过滤adb logcat输出的方法

几种在shell命令行中过滤adb logcat输出的方法 分类标签: LogCat ADB 我们在Android开发中总能看到程序的log日志内容充满了屏幕&#xff0c;而真正对开发者有意义的信息被淹没在洪流之中&#xff0c;让开发者无所适从&#xff0c;严重影响开发效率。本文就具体介绍几种在sh…

duration java_Java Duration类| toHours()方法与示例

duration javaDuration Class toHours()方法 (Duration Class toHours() method) toHours() method is available in java.time package. toHours()方法在java.time包中可用。 toHours() method is used to convert this Duration into the number of hours. toHours()方法用于…

SpringBoot时间格式化的5种方法!

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在我们日常工作中&#xff0c;时间格式化是一件经常遇到的事儿&#xff0c;所以本文我们就来盘点一下 Spring Boot 中时间格…

C#文件加密和解密

下载 CSDN下载&#xff1a;https://download.csdn.net/download/myinc/9913318 Github&#xff1a;GitHub 如果没有积分&#xff0c;也可以关注我获取哟~【文件加密】 // * 最近看了一下加密算法&#xff0c;对加密文件突然很感兴趣&#xff0c;就研究了一下&#xff1a;…

zabbix server 迁移步骤

zabbix server 迁移步骤&#xff1a; 1.在新机器上安装同版本的zabbix server软件和zabbix agent软件。 2.同步zabbix_server.conf配置文件。 3.同步/usr/lib/zabbix/{alertscripts,externalscripts}里面的程序。 4.我们这里有安装使用oneproxy&#xff0c;需要同步oneproxy软件…