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

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


相关推荐

  • FormatDateTime说解[通俗易懂]

    描述返回一个日期或时间格式的表达式。语法FormatDateTime(Date[,NamedFormat])FormatDateTime函数语法有如下几部分:部分描述Date必需的。要被格式化的日期表达式。NamedFormat可选的。数字值,表示日期/时间所使用的格式。如果忽略该值,则使用vbGeneralDate。设置值N…

    2022年4月10日
    57
  • lscpu命令详解

    lscpu的使用描述:  此命令用来显示cpu的相关信息  lscpu从sysfs和/proc/cpuinfo收集cpu体系结构信息,命令的输出比较易读  命令输出的信息包含cpu数量,线程,核数,套接字和Nom-UniformMemeorAccess(NUMA),缓存等  不是所有的列都支持所有的架构,如果指定了不支持的列,那么lscpu将打印列,但不显示数据语法:

    2022年4月18日
    48
  • go mod 在使用私有gitlab时“go-get=1”错误解决

    go mod 在使用私有gitlab时“go-get=1”错误解决

    2021年6月21日
    232
  • 【Quartz】定时器初步实验(一)

    【Quartz】定时器初步实验(一)以前就了解了Quartz这个定时框架,但是一直没有认真的去关注他,最近忽然看到已经更新到3.0.4支持异步操作了所以就写个简单的小例子看看好用不。第一步:创建项目创建一个新项目,可以是ASP.NE

    2022年7月1日
    26
  • for while循环语句举例python_for循环语句python

    for while循环语句举例python_for循环语句python程序在一般情况下是按顺序执行的。编程语言提供了各种控制结构,允许更复杂的执行路径。循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环语句的一般形式1.循环控制语句在了解循环语句的使用方法之前,我们先来了解几个循环控制语句:1)…

    2022年8月12日
    6
  • 手机发布版sha1怎么获取_android获取真实时间

    手机发布版sha1怎么获取_android获取真实时间Android安卓获取发布版SHA1生成打包密钥后打开黑窗口,进入打包密钥同级目录输入命令:keytool-list-v-keystore密钥名称例:keytool-list-v-keystoreappKey回车,输入密钥密码,获取发布版SHA1结束…

    2022年8月11日
    4

发表回复

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

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