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


相关推荐

  • C#多线程同步事件及等待句柄

    C#多线程同步事件及等待句柄最近捣鼓了一下多线程的同步问题,发现其实C#关于多线程同步事件处理还是很灵活,这里主要写一下,自己测试的一些代码,涉及到了AutoResetEvent和ManualResetEvent,当然还有也简要提了一下System.Threading.WaitHandle.WaitOne、System.Threading.WaitHandle.WaitAny和System.Threading.Wait

    2022年7月15日
    15
  • SAXReader从输入流中读取XML文件

    SAXReader从输入流中读取XML文件Mapmap newHashMap nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 从 request 中取得输入流 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp InputStreami request getInputStre nbsp nbsp nbsp 读取输入流 nbsp nbsp nbsp nbsp nbsp nbsp nbsp SAXReaderrea newSAXReader nbsp nbsp nbsp nbsp nbsp nbsp 生成 document 实体

    2025年11月14日
    3
  • YUI中js的继承示例

    YUI中js的继承示例无标题文档一个简单的例子。在这个例子中,可以看到,用var定义的私有变量,是不能被继承的。所有能被继承的,一定是通过this关键字,在内在地址中和这个对象的地址捆在一起的变量。因为复合对象传的不是值

    2022年7月4日
    21
  • 如果我说熟悉SpringBoot 面试官会怎么问?

    如果我说熟悉SpringBoot 面试官会怎么问?SpringBoot因简化了Spring框架使用难度,极大地提高了Java企业级应用开发的效率,成为企业考核人才的重要标准之一。但随着现今互联网行业快速发展、企业业务不断深入,相应地对SpringBoot技术要求也愈来愈高。春节时期有一位打算金三银四面试的读者私信问我:如果我说熟悉SpringBoot面试官会怎么问?​可能不少朋友跟他一样,不清楚当下企业真实生产环境下对SpringBoot有哪些具体要求,需要掌握到什么程度。为此,结合这些年的面试经历及各大厂的职位要求,给

    2022年6月5日
    29
  • 常用的Java基本代码汇总

    常用的Java基本代码汇总1.字符串和整型的相互转换Stringa=String.valueOf(2);inti=Integer.parseInt(a);2.向文件末尾添加内容BufferedWriterout=null;try{out=newBufferedWriter(newFileWriter(“filename”,true));out.write(“iam

    2022年7月8日
    22
  • 转 【TTS】AIX平台数据库迁移到Linux–基于RMAN(真实环境)

    转 【TTS】AIX平台数据库迁移到Linux–基于RMAN(真实环境)

    2022年3月2日
    44

发表回复

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

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