Android Bundle类

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(){
    		@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>

结果:

Android Bundle类

跳转结果:

Android Bundle类


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118124.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JavaSE基础(32) 遍历数组的3种方式

    JavaSE基础(32) 遍历数组的3种方式第一种:for循环//遍历数组publicclassThroughTheArray{ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub int[]arr={12,4,1,66,54,6,74,-3};//静态创建一个数组 for(inti=0;i<…

    2026年1月21日
    2
  • kafuka生产者和消费者及配置

    kafuka生产者和消费者及配置#kafka生产者配置#kafka集群kafka.bootstrap.servers=ip:端口#发送端确认模式kafka.acks=all#发送失败重试次数kafka.retries=10#批处理条数kafka.batch.size=16384#延迟统一收集,产生聚合,然后批量发送kafka.linger.ms=100#批处理缓冲区kafka.buffer.memo…

    2022年6月3日
    52
  • webmin纯web界面管理linux系统「建议收藏」

    webmin纯web界面管理linux系统「建议收藏」关键字:摘要:从Windows环境的管理转到Linux环境的管理时所面临的挑战之一是,您需要去学习利用新的工具。作为一个管理员,您希望理解操作系统的细节以发挥它的最大功效。但是,当您还处在学习阶段时

    2022年8月5日
    5
  • 如何分析系统平均负载过高的原因_线上cpu负载过高如何排查

    如何分析系统平均负载过高的原因_线上cpu负载过高如何排查前言我相信你应该用过uptime命令查询系统负载的情况,或者在各种监控终端上看到过系统load这一项,但是每次问别人到底什么是系统load?系统load到达多少算过高?又有哪些原因会造成系统load过载?我发现很少有人能回答清楚,大多数都觉得系统load过载就表示CPU使用率过载、然而实际上并不完全这样的,本文就来仔细分析一下到底有哪些原因会造成系统load过载!uptime命令还是先来看看uptime命令,…

    2025年11月5日
    3
  • 网络虚拟化和SDN

    1、什么是虚拟化2、服务器虚拟化3、网络虚拟化4、云计算跟网络虚拟化的结合5、SDN在网络虚拟化和云计算中的作用一、什么是虚拟化虚拟化技术,就是将一个物理实体,通过一些复用的技术,克隆出多个虚拟的等价物,这些虚拟的等价物依附于物理的实体之上,共享物理实体的各种资源。类似于进程与线程的关系,在一个进程内可以fork出多个线程,所有线程共享进程的内存资源。提到虚拟化技术,现在通常是指服…

    2022年4月7日
    86
  • quartus ii安装教程9.0激活成功教程教程_quartus ii 13.1安装教程

    quartus ii安装教程9.0激活成功教程教程_quartus ii 13.1安装教程一、首先是QuartusII13.0.1软件的下载如果你没有那么高的要求,用个低版本的QuartusII就足够了,而且低版本的软件比较稳定,为了免去大家找安装文件版本号不匹配的情况,我在这里把我所用的QuartusII13.0.1版本的源安装文件、激活成功教程文件和器件库(Cyclone,CycloneII,CycloneIII,CycloneIVdevices…

    2022年10月15日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号