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

(0)
上一篇 2021年12月4日 上午8:00
下一篇 2021年12月4日 上午8:00


相关推荐

  • free video java hd_0326 iframe和video experience

    free video java hd_0326 iframe和video experience1form表单标签:五个属性2name、action、method、enctype34提交方式:”method”(默认get提交)”>51.get:6所有表达方式以键值成对的方式key=value传递后台。所有信息都在地址栏全部显示url:72.post不会显示表单数据安全,传递内容大小无限制8————————-…

    2022年7月7日
    41
  • Python编程题2–水仙花数

    Python编程题2–水仙花数题目如果一个3位数等于其各位数字的立方和,则称这个数为水仙花数。例如:153=13+53+3^3,因此153就是一个水仙花数请按照从小到大的顺序输出1000以内的水仙花数

    2022年7月5日
    23
  • s一般怎么称呼自己的m_怎么判断自己的肾脏是否健康?肾好的人,一般会有这8大特征…

    s一般怎么称呼自己的m_怎么判断自己的肾脏是否健康?肾好的人,一般会有这8大特征…肾脏是身体的“净化器”,经过肾脏代谢排毒之后,废物顺着尿液排出体外,使内环境得到净化。可见,拥有一颗健康的肾脏是非常重要的,而我们日常生活中也要注意对肾脏健康的自我检查。那么,我该如何粗略自我判断自己肾脏是否健康呢?接下来就让我们一起来了解一下肾脏健康情况下,身体会有哪些表现。肾脏健康有哪些表现?1、小便没有泡沫:日常生活中小便时在尿液中没有出现泡沫且颜色清澈正常,那么往往说明肾脏也是比较健康的。…

    2022年6月23日
    32
  • Java1.8安装详细教程[通俗易懂]

    Java1.8安装详细教程[通俗易懂]JavaJDK安装与配置1、下载官网地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html(1)点击DOWNLOAD(2)选择相应版本、因为我的是windows64位所以选择最下方的390.25MB的版本(3)下载后一直点击下一步就好了ps:路径不喜欢安装在C盘的可以选择…

    2022年7月16日
    15
  • 2021 Java面试真题集锦

    2021 Java面试真题集锦目录…1大厂面试的基本流程 17字节跳动 17阿里 17腾讯 18网易游戏 18面试前需要准备: 18面试中可以借鉴的套路 19用简历争取到更多的面试机会 19自我介绍时,立即全面抛出技术栈和亮点 20先介绍项目背景,打消面试官疑问 21通过说项目管理工具,说明你不仅会写代码 22用SPRINGBOOT项目举例,说明你的技术基本面 23用实例说明你在内存调优方面的经验 24展示你在数据库调优方面的经验 25总结前文说辞 26准备项目说辞时,更可以准备后继面试官的问

    2022年7月8日
    54
  • 什么是高并发 ,详细讲解[通俗易懂]

    什么是高并发 ,详细讲解[通俗易懂]一、什么是高并发高并发(HighConcurrency)是互联网分布式系统架构设计中必须考虑的因素之一,它通常是指,通过设计保证系统能够同时并行处理很多请求。 高并发相关常用的一些指标有响应时间(ResponseTime),吞吐量(Throughput),每秒查询率QPS(QueryPerSecond),并发用户数等。 响应时间:系统对请求做出响应的时间。例如系统处理一…

    2022年5月31日
    37

发表回复

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

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