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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Django组件_tuxedo中间件

    Django组件_tuxedo中间件什么是Django中间件中间件(Middleware)是一个用来处理Django的请求(Request)和响应(Response)的框架级别的钩子,它是一个轻量、低级别的插件系统,用于在全局范围内改

    2022年8月7日
    7
  • VMware虚拟机安装xp系统

    VMware虚拟机安装xp系统安装vmware安装xpxp使用微软原版,vm版本15.5安装后,可以连接网络。资源提取码:链接:https://pan.baidu.com/s/1Vxrds1rjRMGcRjlGaDVy0Q提取码:0101–来自百度网盘超级会员V2的分享要实现文件共享,需要关机,然后设置,允许共享,然后开机,就可以了。…

    2022年8月16日
    5
  • java.math.BigDecimal 比较大小

    java.math.BigDecimal 比较大小BigDecimala=newBigDecimal(101);BigDecimalb=newBigDecimal(111);//使用compareTo方法比较//注意:a、b均不能为null,否则会报空指针if(a.compareTo(b)==-1){System.out.println("a小于b");}if(a.compareTo(b)==…

    2022年7月14日
    31
  • OpenSSL密码库算法笔记——第5.2章 椭圆曲线算法的函数架构图

    OpenSSL密码库算法笔记——第5.2章 椭圆曲线算法的函数架构图椭圆曲线算法中涉及的函数纷繁复杂,比如为了实现“复制点群”功能,就定义了四个函数,有:intEC_GROUP_copy(EC_GROUP*dest,constEC_GROUP*src)、intec_GFp_mont_group_copy(EC_GROUP*dest,constEC_GROUP*src)、intec_GFp_simple_group_copy(…

    2022年7月20日
    12
  • Python打包exe文件方法汇总【4种】

    Python打包exe文件方法汇总【4种】title:Python打包exe文件方法copyright:truetop:0date:2018-08-1121:08:21tags:打包categories:Python进阶笔记permalink:password:keywords:description:使用py2exe,pyinstaller,cx_Freeze,nuitka对python文件进行打包,…

    2022年4月29日
    106
  • linux 虚拟ip 漂移,keepalived 虚拟ip切换

    linux 虚拟ip 漂移,keepalived 虚拟ip切换简介 KeepalivedKe 是运行在 lvs 之上 是一个用于做双机热备 HA 的软件 它的主要功能是实现真实机的故障隔离及负载均衡器间的失败切换 提高系统的可用性 虚拟 ip 虚 IP 就是一个未分配给真实主机的 IP 也就是说对外提供数据库服务器的主机除了有一个真实 IP 外还有一个虚 IP 使用这两个 IP 中的任意一个都可以连接到这台主机 所有项目中数据库链接一项配置的都是这个虚 IP

    2025年8月2日
    5

发表回复

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

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