バンドルは、アクティビティ間でデータを交換するために使用しました

(A)バンドル紹介

データを送信するために主にバンドル、それはデータを保存し、それがキー値(キーと値のペア)の形で存在します。

私たちはしばしばバンドル活性との間のデータ転送に使用される、データ転送がブール値、バイト、整数、長い、フロート、ダブル、文字列、または実質的にアレイの対応する他のタイプであってもよく、オブジェクトは、オブジェクトまたは配列であってもよいです。

バンドルは、渡されたオブジェクトまたはオブジェクトの配列は、実装しなければならないときである場合シリアライズまたはParcelableインターフェイスを。以下は、オブジェクトを渡し、活動の基本的なタイプの間を通過する方法について説明します。

1.基本的なデータ転送

バンドルは、基本的なタイプのデータを読み取り、書き込むためputXxx()/のgetXXX()メソッドの一般的に使用される様々なタイプのを提供します。次の表に示すように、基本的なデータ型のバンドルAPIオペレーション:

 

 

次のようにデータを書き込む方法は、次のとおりです。

btn.setOnClickListener(新しいView.OnClickListener(){
            @オーバーライド
            公共 のボイドのonClick(ビューV){
                ストリングsate1 = ((のEditText)findViewById(R.id.site1))のgetText()のtoString()。。。
                ストリング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();
            }
        });
    }
}

初始界面:

 传递数据后的页面:

 

 

おすすめ

転載: www.cnblogs.com/xiaofengzai/p/12275168.html