4.Android学习之基本程序单元Activity(二)

目录

4.基本程序单元Activity(二)

3.多个Activity的使用

3-1.使用Bundle在Activity之间交换数据

3-2.调用另一个Activity并返回结果


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_
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值