使用Bundle在Activity之间交换数据

(一)Bundle介绍

Bundle主要用于传递数据;它保存的数据,是以key-value(键值对)的形式存在的。

我们经常使用Bundle在Activity之间传递数据,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。

当Bundle传递的是对象或对象数组时,必须实现Serializable 或Parcelable接口。下面分别介绍Activity之间如何传递基本类型、传递对象。

1.传递基本数据

Bundle提供了各种常用类型的putXxx()/getXxx()方法,用于读写基本类型的数据。Bundle操作基本数据类型的API表格如下所示:

写数据的方法如下:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sate1=((EditText)findViewById(R.id.site1)).getText().toString();
                String sate2=((EditText)findViewById(R.id.site2)).getText().toString();
                String sate3=((EditText)findViewById(R.id.site3)).getText().toString();
                String phone=((EditText)findViewById(R.id.phone)).getText().toString();
                String name=((EditText)findViewById(R.id.name)).getText().toString();

                if(!"".equals(sate1)&&!"".equals(sate2)&&!"".equals(sate3)&&!"".equals(phone)&&!"".equals(name)){
                    Intent intent=new Intent(MainActivity.this,AddressActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("name",name);
                    bundle.putString("phone",phone);
                    bundle.putString("sate",sate1+sate2+sate3);
                    intent.putExtra("bundle",bundle);
                    startActivity(intent);
                }else{
                    Toast.makeText(MainActivity.this,"请将信息填写完整",Toast.LENGTH_SHORT).show();
                }
            }
        });

对应的读数据的方法如下:将读取的数据设置给TextView组件

Intent intent=getIntent();
        Bundle bundle=intent.getBundleExtra("bundle");

        TextView site=(TextView) findViewById(R.id.site);
        TextView name=(TextView)findViewById(R.id.name);
        TextView phone=(TextView)findViewById(R.id.phone);

        site.setText(bundle.getString("sate"));
        phone.setText(bundle.getString("phone"));
        name.setText(bundle.getString("name"));

我们根据所学的hundle的知识,来简单的制作一个案例:实现通过bundle进行activity之间的数据传递

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="190dp"
        android:background="@drawable/top"
        app:layout_constraintBottom_toTopOf="@+id/site3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <EditText
        android:id="@+id/site1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="71dp"
        android:hint="请输入省份"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/site2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入市"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/site1" />

    <EditText
        android:id="@+id/site3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:hint="请输入县"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/site2" />

    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:hint="请输入手机电话"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/site3" />

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="22dp"
        android:hint="请输入姓名"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/phone" />

    <Button
        android:id="@+id/btnok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="98dp"
        android:background="#045786"
        android:text="保存"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_address.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".AddressActivity">

    <ImageView
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:src="@drawable/guanbi"
        app:layout_constraintBottom_toBottomOf="@+id/imageView2"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:src="@drawable/top"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="66dp"
        android:text="收货姓名"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="电话"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/name" />

    <TextView
        android:id="@+id/site"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="68dp"

        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="@+id/phone"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/phone" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn=(Button) findViewById(R.id.btnok);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sate1=((EditText)findViewById(R.id.site1)).getText().toString();
                String sate2=((EditText)findViewById(R.id.site2)).getText().toString();
                String sate3=((EditText)findViewById(R.id.site3)).getText().toString();
                String phone=((EditText)findViewById(R.id.phone)).getText().toString();
                String name=((EditText)findViewById(R.id.name)).getText().toString();

                if(!"".equals(sate1)&&!"".equals(sate2)&&!"".equals(sate3)&&!"".equals(phone)&&!"".equals(name)){
                    Intent intent=new Intent(MainActivity.this,AddressActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("name",name);
                    bundle.putString("phone",phone);
                    bundle.putString("sate",sate1+sate2+sate3);
                    intent.putExtra("bundle",bundle);
                    startActivity(intent);
                }else{
                    Toast.makeText(MainActivity.this,"请将信息填写完整",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

AddressActivity.java

package com.example.bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AddressActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);

        Intent intent=getIntent();
        Bundle bundle=intent.getBundleExtra("bundle");

        TextView site=(TextView) findViewById(R.id.site);
        TextView name=(TextView)findViewById(R.id.name);
        TextView phone=(TextView)findViewById(R.id.phone);

        site.setText(bundle.getString("sate"));
        phone.setText(bundle.getString("phone"));
        name.setText(bundle.getString("name"));

        ImageView close=(ImageView) findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

初始界面:

传递数据后的页面:

原文地址:https://www.cnblogs.com/xiaofengzai/p/12275168.html

时间: 2024-08-06 14:32:16

使用Bundle在Activity之间交换数据的相关文章

Bundle在Activity之间交换数据

在Activity之间进行数据交换有一个"信使":Intent,因此主要是将需要交换的数据放入Intent即可 Intent提供了多个重载方法,"携带"额外的数据: 放入Bundle数据: putExtras(Bundle data);向Intent中放入需要"携带"数据包 Bundle getExtras();取出Intent所"携带"的数据包 放入键值数据:(源码底层实际上还是使用的Bundle) putExtra(Str

Activity之间的数据传递-android学习之旅(四十七)

activity之间的数据传递主要有两种,一种是直接发送数据,另一种接受新启动的activity返回的数据,本质是一样的 使用Bundle传递数据 Intent使用Bundle在activity之间传递数据. 代码示例 dto类person package peng.liu.test; import java.io.Serializable; /** * Created by lplp on 2015/6/22. */ public class Person implements Seriali

安卓Fragment和Activity之间的数据通讯

Fragment是Android3.0之后才推出来的.可以用来做底部菜单,现在很多APP都有用到这个底部菜单.当然TabHost也可以用来做底部菜单,但是Fragment来做,动画效果这些可以做得更炫. Fragment和Activity之间是可以相互进行参数传送这些.但是Fragment不可以直接就将数据传送到另外一个Fragment,而是借助Activity,先传到Activity,如何再通过Activity传到Fragment.  所以实现Fragment和Activity之间的数据传送是

Intent 意图 用于Activity之间的数据传递

转自:http://blog.sina.com.cn/s/blog_83940dfb0100veas.html 用于Activity之间的数据传递 在起始Activity中,发送数据 protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.thisactivity); Intent intent = new Intent(); //设

(Android review)Activity之间的数据传递

一.基本知识点 1.Activity之间传递数据1)传递基本类型或String intent.putExtra("username", username);  getIntent(); intent.getStringExtra("username"); 2)以bundle的形式传 Bundle bundle = new Bundle();    bundle.putString("username", username);    bundle.

Activity之间的数据传递

最常用的Activity之间的数据传递. 1 btnStartAty1.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View v) { 4 Intent i=new Intent(MainActivity.this, Activity1.class); 5 //Activity之间的数据传递 6 //i.putExtra("txt", "这是第一种Activ

Android 笔记-Fragment 与 Activity之间传递数据

Fragment 与 Activity之间传递数据有两种方法,一种是使用setArgument,一种是使用接口回调.下面先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这里打个比喻:假如Activity是皇帝,它设立了三个部门(如三省六部),分别是Fragment1,Fragment2和Fragemnt3: 现在他现在要吩咐部门Fragment1去做一些事情,比如说:领兵攻打岛国!!好,它肯定不自己跑去告诉该部门的. 一般来说,会有个宰相或者太监总管来负责皇帝口谕是

两个Activity之间共享数据、互相访问的另一种方式的实现

本帖最后由 勇敢的心_ 于 2010-9-29 11:51 编辑本人从windows编程转过来学习Android开发,一直在想如果两个Activity之间能够像C#或delphi中的Form一样,可以直接访问其成员(字符.数值.成员对象等),并能调用其公开的方法,那应该比用Intent来传递数据直接方便的多,于是偿试了如下办法,测试基本没有问题,发出来大家讨论一下.本人学习android不久,幼稚的地方希望大家不要见笑原理:假设有两个Activity:ActivityMain 和 Activit

Android笔记Fragment与Activity之间传递数据

Fragment 与 Activity之间传递数据有两种方法,一种是使用setArgument,一种是使用接口回调.下面先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这里打个比喻:假如Activity是皇帝,它设立了三个部门(如三省六部),分别是Fragment1,Fragment2和Fragemnt3: 现在他现在要吩咐部门Fragment1去做一些事情,比如说:领兵攻打岛国!!好,它肯定不自己跑去告诉该部门的. 一般来说,会有个宰相或者太监总管来负责皇帝口谕是