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/118374.html原文链接:https://javaforall.net

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

相关推荐

  • 一种成熟的MODBUS调试测试工具助手上位机软件(MThings) 免费中文

    一种成熟的MODBUS调试测试工具助手上位机软件(MThings) 免费中文一种成熟的MODBUS调试测试工具助手软件(MThings)免费中文现有MODBUS调测软件种类丰富,基本可以满足日常调测需求,但是面对用户群体对高效灵活友好的进一步需求都存在着差距。MThings是一款全新的标准化MODBUS调测工具,提供主从机一体化操作。全功能覆盖MODBUSPollSlave,功能全网最强。

    2022年5月31日
  • windows security notification_windows product activation

    windows security notification_windows product activation来源:《WCF服务编程(第三版)》

    2022年10月7日
  • FFmpeg从入门到精通(一)

    FFmpeg从入门到精通(一)FFmpeg介绍FFmpeg既是一种音视频编解码工具,也是一组音视频解码开发套件。作为编解码开发套件,它为开发者提供了丰富的音视频处理的调用接口。FFmpeg基本组成先看图,再看字。FFmpeg的封装模块AVFormatAVFormat中实现了目前多媒体领域中大多数媒体封装格式,包括封装和解封装,如:MP4,FLV等文件封装格式、HTTP,RTMP,HLS等网络协议封装格式。FFm…

    2022年6月26日
  • iOS友盟消息推送总是推送失败或者token无效[通俗易懂]

    iOS友盟消息推送总是推送失败或者token无效[通俗易懂]最近遇到的问题,仅仅贴出来我的解决办法问题(开发环境下)我的原因AppleDevCenter中的开发者provisioningfile无效了,重新生成了一个就OK。选择iOSAppDevelopment然后一步步生成一个,下载到本地,双击安装。然后,手动的在buildSetttings中,修改codesign(因为我的电脑中有三个AppStore账号的证书,所以手动靠谱些

    2022年9月12日
  • VMware Ubuntu安装详细过程(详细图解)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家!一.下载Ubuntu镜像文件下载地址:http://mirrors.aliyun.com/ubuntu-releases/16.04/进入下载页面,如下图选择版本点击即可下载二.下载及安装VMware下载地址:https://pan.baidu.com/s/1aEEI-DRa4oKeViddxW2CPA…

    2022年4月7日
  • 字符数组反转_字符数组的初始化

    字符数组反转_字符数组的初始化关于字符串的反转,倒是很简单,但是编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组char[]的形式给出。不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用O(1)的额外空间解决这一问题。你可以假设数组中的所有字符都是ASCII码表中的可打印字符。示例1:输入:[“h”,“e”,“l”,“l”,“o”]输出:[“o”,“l”,“l”,“e”,“h…

    2022年5月3日

发表回复

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

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