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)
上一篇 2021年12月7日 上午7:00
下一篇 2021年12月7日 上午8:00


相关推荐

  • 紫光同创国产FPGA学习之器件情况

    紫光同创国产FPGA学习之器件情况国产的 FPGA 目前比较少 年轻没有办法的 所以 器件也没什么好选的 两个系列 titan 和 logos 好像 titan 系列的芯片 一般情况买不到 估摸着没点量 人家不卖给你 好吧 只能选 logos 系列了 TBD 是待决定的意思 12G 35H 50H 待决定 那就只有 22G 唯一的一种了 只有 2 个封装 186 240 管脚 好像比 xc6slx25 小了那么一点点 差不多一样的资源量吧

    2026年3月26日
    1
  • css让图片居中

    css让图片居中css 让图片居中 不管图片是方图 竖图 横图 都可以让图片在父框架下居中 即方图则占满整个父框架 横图则左右填充上下居中 竖图则左右居中上下填充 1 html 如下 这里的图片路径自己设置 2 css 如下 body background color gray red

    2026年3月19日
    1
  • 【超分辨】SRGAN详解及其pytorch代码解释

    【超分辨】SRGAN详解及其pytorch代码解释SRGAN 是一个超分辨网络 利用生成对抗网络的方法实现图片的超分辨 本文解释了 SRGAN 原理 同时通过 pytorch 代码实现

    2025年10月14日
    7
  • mysql workbench如何导入数据库_sql数据库脚本导入

    mysql workbench如何导入数据库_sql数据库脚本导入首先,打开MySQLworkbench,先新建数据库(我们会把.sql文件导入之这个数据库),新建数据库过程如下:先点击1处,新建数据库,给数据库起个名字,点击appy,就创建成功了。之后点击2处,就可以看到现有的数据库了。这里圈出来的是我新建的数据库,双击选中这个数据库(双击选中很重要,因为选中之后才能将.sql文件导入这个数据库中)。然后点击左上方的第二个图标(图中圈出来的那…

    2022年10月2日
    6
  • python中cPickle用法「建议收藏」

    python中cPickle用法「建议收藏」在python中,一般可以使用pickle类来进行python对象的序列化,而cPickle提供了一个更快速简单的接口,如python文档所说的:“cPickle–Afasterpickle”。cPickle可以对任意一种类型的python对象进行序列化操作,比如list,dict,甚至是一个类的对象等。而所谓的序列化,我的粗浅的理解就是为了能够完整的保存并能够完全可逆的恢复。在cPi…

    2022年6月29日
    26
  • 国外期货程序化交易之行情获取讲解

    国外期货程序化交易之行情获取讲解开发的期货行情程序及交易程序 主要还是和郑州易盛的交易系统对接 基于易盛的 sdk 做二次开发 通过调用易盛的行情 api 获取期货合约行情 调用易盛的交易 api 完成交易报单 当然也可以申请美国盈透的账号 然后使用盈透的行情及交易 api

    2026年3月18日
    2

发表回复

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

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