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


相关推荐

  • 基于Pycharm的Django学习1 —— Django三种响应

    基于Pycharm的Django学习1 —— Django三种响应Flask学完啦,那就来学Django吧,学习真的是会上瘾的奥!由于Flask比较简单,而且Django和Flask有很多基础语法也是有相通之处的,所以就不写Flask的博客了,一起学Django吧!基于Pycharm的Django学习Python-WebDjango的三种响应响应文本内容响应html页面响应重定向Python-Web其实在上一篇博客中,讲解Pycharm社区版创建Django项目的时候,已经讲了项目的目录结构,以及每一个文件主要是用来干什么的。但是还没有和前端联动,现在在项

    2022年9月7日
    0
  • pycharm2021年激活码刚出【注册码】

    pycharm2021年激活码刚出【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    38
  • 动画图解:十大经典排序算法动画与解析,看我就够了!(配代码完全版)[通俗易懂]

    动画图解:十大经典排序算法动画与解析,看我就够了!(配代码完全版)[通俗易懂]排序算法是《数据结构与算法》中最基本的算法之一。排序算法可以分为内部排序和外部排序。内部排序是数据记录在内存中进行排序。而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。常见的内部排序算法有:插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。用一张图概括:时间复杂度与空间复杂度关于时间复杂度:…

    2022年10月9日
    0
  • 解决WINXP系统开机后弹出Generic host process for win32 services 遇到问题需要关闭![通俗易懂]

    解决WINXP系统开机后弹出Generic host process for win32 services 遇到问题需要关闭![通俗易懂]解决WINXP系统开机后弹出Generichostprocessforwin32services遇到问题需要关闭!出现上面这个错误一般有三种情况。1.就是病毒。开机后会提示GenericHostProcessforWin32Services遇到问题需要关闭”“RemoteRrocedureCall(RPC)服务意外终止,然后就自动重起电脑。一般该病毒会在注

    2022年10月9日
    0
  • esp-idf的内存管理——tlsf算法

    esp-idf的内存管理——tlsf算法目录1最初还不是tlsf2为什么要引入tlsf3idf中使用的tlsf算法的设计与实现4源码走读参考1最初还不是tlsf2为什么要引入tlsf3idf中使用的tlsf算法的设计与实现4源码走读参考[1]半文钱的博客[2]upstream所在的github地址注意事项放到内存调试去说:用户需要关注的:内存的硬件特性(caps)内存的访问速度内存是否支持原子操作内存是否可以由CPU直接访问用户在使用时:用户自己也要对自己的应用需要使用的内存做一些安排,有的内存比

    2022年6月29日
    22
  • HashMap扩容全过程

    HashMap扩容全过程 1.如果HashMap的大小超过了负载因子(loadfactor)定义的容量,怎么办?默认的负载因子大小为0.75,也就是说,当一个map填满了75%的bucket时候,和其它集合类(如ArrayList等)一样,将会创建原来HashMap大小的两倍的bucket数组,来重新调整map的大小,并将原来的对象放入新的bucket数组中。这个过程叫作rehashing,因为它调用hash方…

    2022年9月2日
    5

发表回复

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

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