Android基础——使用Bundle在Activity间传递数据

时间:2021-11-02 18:10:11

Bundle是个保存元组<key,value>的数据结构,两个活动传递数据时就通过Bundle进行传递

在Main中输入数据,然后将数据传递给Address

两个活动布局

Main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <EditText
        android:id="@ id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入姓名"
        />
    <EditText
        android:id="@ id/classname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入班级名"
        />
    <EditText
        android:id="@ id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入学号"
        />

    <Button
        android:id="@ id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        />

</LinearLayout>

Address

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >

    <TextView
        android:id="@ id/_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@ id/_classname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@ id/_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@ id/_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回"
        />
</LinearLayout>

Main的java调用

package com.example.myactivityii;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
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 button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //获取EditText中的内容
                String site1 = ((EditText)findViewById(R.id.name)).getText().toString();
                String site2 = ((EditText)findViewById(R.id.classname)).getText().toString();
                String site3 = ((EditText)findViewById(R.id.number)).getText().toString();
                if("".equals(site1) || "".equals(site2) || "".equals(site3)){//只要有一个信息为空
                    Toast.makeText(
                            MainActivity.this,"请将信息填写完整",Toast.LENGTH_SHORT
                    );
                }
                else {
                    //1.创建Intent对象
                    Intent intent = new Intent(
                            MainActivity.this, AddressActivity.class
                    );
                    //2.创建Bundle对象
                    Bundle bundle = new Bundle();
                    //3.在Bundle里保存信息
                    bundle.putCharSequence("name,",site1);//保存的是字符序列
                    bundle.putCharSequence("classname",site2);
                    bundle.putCharSequence("number",site3);
                    //4.将Bundle保存到Intent中去
                    intent.putExtras(bundle);
                    //5.启动intent,将信息传递到AddressActivity
                    startActivity(intent);
                }
            }
        });



    }
}

Address的java调用

package com.example.myactivityii;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

/*
* 获取Main中传递的Intent并处理其中的信息
* */
public class AddressActivity extends AppCompatActivity {

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

        //1.获取Intent和Bundle
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        //2.获取信息
        String name = bundle.getString("name");
        String classname = bundle.getString("classname");
        String number = bundle.getString("number");
        //3.更新到当前活动中
        TextView textView1 = (TextView)findViewById(R.id._name);
        TextView textView2 = (TextView)findViewById(R.id._classname);
        TextView textView3 = (TextView)findViewById(R.id._number);
        textView1.setText(name);
        textView2.setText(classname);
        textView3.setText(number);

    }
}