使用Bundle在Activity之間交換數據

(一)Bundle介紹java

Bundle主要用於傳遞數據;它保存的數據,是以key-value(鍵值對)的形式存在的。android

咱們常用Bundle在Activity之間傳遞數據,傳遞的數據能夠是boolean、byte、int、long、float、double、string等基本類型或它們對應的數組,也能夠是對象或對象數組。數組

當Bundle傳遞的是對象或對象數組時,必須實現Serializable 或Parcelable接口。下面分別介紹Activity之間如何傳遞基本類型、傳遞對象。app

1.傳遞基本數據

Bundle提供了各類經常使用類型的putXxx()/getXxx()方法,用於讀寫基本類型的數據。Bundle操做基本數據類型的API表格以下所示:ide

 

 

寫數據的方法以下:this

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組件spa

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之間的數據傳遞.net

activity_main.xml3d

 

<?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.xmlcode

<?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();
            }
        });
    }
}

初始界面:

 傳遞數據後的頁面:

 

相關文章
相關標籤/搜索