目录
4.基本程序单元Activity(二)
3.多个Activity的使用
3-1.使用Bundle在Activity之间交换数据
当在一个Activity中启动另一个Activity时,经常需要传递一些数据。 这时就可以通过Intent来实现,因为Intent通常被称为是两个Activity之间的信使,通过将要传递的数据保存在Intent中,就可以将其传递到另一个Activity中了。在Android中可以将要保存的数据存放在Bundle对象中,然后通过Intent提供的putExtras()方法将要携带的数据保存到Intent中。
注:Bundle 是一个key-value (键-值)对的组合,用于保存要携带的数据包。这些数据可以是boolean、byte、int、long、float、double 和String等基本类型或者对应的数组,也可以是对象或者对象数组。如果是对象或者对象数组时,必须实现Serializable或者Parcelable接口。
例:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:src="@drawable/top"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/main_et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:hint="北京市东城区"
android:textSize="24sp" />
<EditText
android:id="@+id/main_et2"
android:layout_marginTop="100dp"
android:hint="东华门街道"
android:textSize="24sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/main_et3"
android:layout_marginTop="150dp"
android:hint="2333号"
android:textSize="24sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/main_et4"
android:layout_marginTop="200dp"
android:hint="李四"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/main_et5"
android:layout_marginTop="250dp"
android:hint="12345678910"
android:textSize="24sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/main_et6"
android:layout_marginTop="300dp"
android:hint="130000"
android:textSize="24sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/main_bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_