在上一节例子中,介绍了如何在Activity
中调用另一个Activity
,但若需要在调用 另外一个Activity
的同时传递数据,那么就需要利用Android.os.Bundle
对象封装数据的能力,将欲传递的数据或参数通过Bundle
来传递不同Intent
之间的数据.
本范例将设计一个简单的个人信息表单,有姓名(EditText
)和性别(RadioButton
)还有一个提交按钮(Button
),当点击提交按钮时,另外一个页面将显示个人信息.
首先我们看一下效果图:


下面是我们所涉及有变动的代码:
首先是主页面布局main.xml
和第二个页面的布局mylayout.xml
main.xml
这里我们用了AbsoluteLayout布局大家 要注意哦
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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="Person information"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Name:"
android:layout_y="30px"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Gender:"
android:layout_y="80px"
/>
<EditText
android:id="@+id/ed1"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_x="50px"
android:layout_y="20px"
/>
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="man"
android:layout_x="60px"
android:layout_y="65px"
android:checked="true"//默认它是被选中的
/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="woman"
android:layout_x="160px"
android:layout_y="65px"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="confirm"
android:layout_y="120px"
/>
</AbsoluteLayout>
在main.xml
同一目录下建另外一个页面布局mylayou.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:id="@+id/mytv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
下面是程序的核心代码BundleDemo.java
和BundleDemo1.java
BundleDemo.java:
package com.android.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class BundleDemo extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//以findViewById()取得Button对象,并添加响应
Button bt1 = (Button)findViewById(R.id.bt1);
bt1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//取得name
EditText et = (EditText)findViewById(R.id.ed1);
String name = et.getText().toString();
//取得Gender
String sex="";
RadioButton rb1 = (RadioButton)findViewById(R.id.rb1);
if(rb1.isChecked())
{
sex="man";
}else{
sex="woman";
}
//new一个Intent对象,用法上节已经讲过了
Intent intent = new Intent();
intent.setClass(BundleDemo.this, BundleDemo1.class);
//new一个Bundle对象,并将要传递的数据导入,这里是重点Map<String,String>结构哦
Bundle bundle = new Bundle();
bundle.putString("name",name);
bundle.putString("sex", sex);
//将Bundle对象assign给Intent
intent.putExtras(bundle);
//调用Activity BundleDemo1
startActivity(intent);
}
});
}
}
在BundleDemo.java
同一目录建立BundleDemo1.java
:
package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class BundleDemo1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//取得Intent中的Bundle对象
Bundle bundle = this.getIntent().getExtras();
//取得Bundle对象中的数据
String name = bundle.getString("name");
String sex = bundle.getString("sex");
//设置输出文字
TextView mytv = (TextView)findViewById(R.id.mytv);
mytv.setText("you name is:" + name + "\n you gender is:"+sex);
}
}
最后我们还是要在AndroidManifest.xml
中加入BundleDemo1
这个Activity
,一定要加,不然后果自负呵呵~
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BundleDemo"
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="BundleDemo1"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
执行之,将达到上述效果~今天到此结束,谢谢大家!
分享到:
相关推荐
在实际开发中,经常需要在Fragment和Activity之间传递数据,这通常是通过Bundle对象来实现的。Bundle类在Android中扮演着数据容器的角色,它允许我们存储键值对的数据,并在不同组件之间共享。 首先,了解Activity...
在Android应用开发中,Activity是用户界面的基本单元,它们之间的数据传递是十分常见的操作。本文将详细介绍Android中Activity之间数据传递的多种方式及其适用场景。 一、Intent传递数据 Intent是Android系统中...
理解如何有效地在两者之间传递数据是提升Android开发技能的关键。 ### 1. Bundle对象的介绍 Bundle是一个键值对的数据结构,它类似于Java中的HashMap,可以存储各种基本数据类型(如int、String、boolean等)以及...
Android 通过Intent使用Bundle传递对象 Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法。 被传递的对象需要先实现序列化,而序列化对象有两种方式:java.io....
本教程将深入探讨如何在Android的Activity之间传递数据,以及如何在启动的Activity销毁后,主Activity能接收到返回的结果。 首先,我们要了解Activity间的启动模式。Android提供了四种启动模式:标准模式(Standard...
在Android中传递数据的方法非常多,本次介绍4中比较常用的数据传递方法: 1.通过Intent传递数据 2.通过静态变量(static)传递数据 3.通过剪贴板(Clipboard)传递数据 4.通过全局对象传递数据 在...
hudi-hadoop-mr-bundle-0.11.0.jar 配合文档
"Activity之间传递数据"这一主题,主要涉及如何使用Intent对象在不同的Activity之间传递各种类型的数据,包括基本数据类型、字符串、数组、集合以及自定义对象等。在Android中,Intent不仅用于启动新的Activity,还...
当我们需要在不同的Activity之间传递数据时,特别是涉及到对象实例的传递,就需要掌握特定的技巧。本篇将深入探讨如何在Android中进行Activity间的对象传递,并以"Test001"为例进行演示。 一、Intent:数据传递的...
当需要在不同的Activity之间传递数据时,我们通常会使用Intent对象,而Intent中的Bundle就是用来存储和传递这些数据的重要工具。本篇文章将深入探讨如何利用Bundle在Android的Activity之间传递消息。 Bundle,全称...
Nexus是Maven社区广泛使用的私有仓库服务器,它允许开发者管理和分发Java库,包括JARs和其他构建工件。"nexus-2.13.0-01-bundle"是一个包含Nexus 2.x系列的最新版本的压缩包,这个版本是2.13.0-01。在Maven项目中,...
官网下载的OSS版:nexus-2.15.1-02-bundle.zip 用于搭建mavne私服 Nexus Repository OSS Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库 下载所需要的构件(artifact),但这通常不是一个好的做法...
Horizon Agent 会将这些设置传递到 Horizon Client,并根据您是指定合并 (m) 还是覆盖 (o) 修改符来解释和执行。Horizon Client 使用这些设置来确定是否将复合 USB 设备拆分为组件设备以及是否禁止组件设备用于...
6. **版本管理**:通过版本管理,Nexus可以帮助团队遵循最佳实践,如使用SNAPSHOT版本进行开发,以及管理不同版本之间的关系。 7. **自动清理**:可以设置定时任务,自动清理不再使用的旧版本工件,节省存储空间。 ...
1. **安装Nexus**: 首先,你需要解压"nexus-2.11.2-03-bundle",然后在服务器上运行提供的启动脚本(如在Unix/Linux系统中使用`bin/nexus start`,在Windows系统中运行`bin/nexus.exe start`)。 2. **配置Nexus**:...
Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。 Nexus不是Maven的核心概念,它仅仅是一种衍生出来的特殊的Maven仓库。对于Maven来说,仓库只有两种:本地仓库和远程仓库。 本地...
在本文中,我们将深入探讨“nexus-2.11.2-03-bundle.tar.gz”这一特定版本的Nexus在Linux环境下的安装与使用。 首先,我们来理解一下“bundle”这个词在Nexus上下文中的含义。在Nexus的上下文中,"bundle"通常指的...
Bundle则是一个用来存储键值对的数据结构,常用于Activity之间传递数据。本示例将深入探讨如何在Android活动中利用Bundle进行通信。 首先,理解Activity的基本概念至关重要。Activity是Android系统中的窗口,它负责...
在Android应用开发中,数据传递是至关重要的一个环节,它涉及到不同组件间的通信,比如Activity、Service、BroadcastReceiver之间的交互。本篇文章将深入探讨四种主要的数据传递方式:Intent、Bundle、Serializable...
在Android应用开发中,Activity是用户界面的基本单元,经常需要在不同的Activity之间传递数据。本篇文章将详细讲解如何在Android的两个Activity之间传递参数,并通过给出的`LoginActivity.java`和`MainActivity.java...