使用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