Bundle类及应用Intent传递数据(Android)

一、Bundle类

Bundl类是为字符串与某组件对象建立映射关系的组件,它与Intent配合使用,可在不同的Activity之间传送数据。常用方法如下:

 1、putString:把字符串用键值对的形式放到Bundle对象中。
 2、remove:移除指定key的值。
 3、getString:获取指定key的值。

二、应用Intent在不同的Activity之间传送数据

1、在界面的Activity A端

(1)在界面的Activity对象和Bundle对象。

  Intent intent=new Intent();
  Bundle bundle=new Bundle();

(2)为Intent指定要跳转的界面,并用Bundle存放键值对数据。

Intent.setClass(MainActivity.thiw.secondActivity.class);
bundle.putString("标记1","要传送的信息内容");
2、在另一界面的Activity B端

(1)从Intent中获取Bundle对象。

 Bundle bb=this.getIntent().getString().getExras();

(2)从Bundle对象中按键值对的键名获取对应的数据值。

String str=rb.getString("标记1");

三、在不同的Avtivity界面之间传递数据

1、实现效果如下:

在这里插入图片描述
在这里插入图片描述

2、布局文件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">-->

    <TextView
        android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里是MainActivity"
        android:textSize="36sp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="72dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.074" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到SecondActivity"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="134dp" />

    <EditText
        android:id="@+id/edit"
        android:layout_width="220dp"
        android:layout_height="48dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="请输入一个词语"
        android:textSize="24sp"
        android:layout_marginTop="41dp"
        app:layout_constraintTop_toBottomOf="@+id/txt2"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
3、布局文件activity_second.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">-->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="欢迎来到 SecondActivity"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.508"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.2" />

    <TextView
        android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="24sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintVertical_bias="0.3" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="248dp"
        android:layout_marginLeft="8dp"
        android:text="返回第一个页面"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.478"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
4、控制文件MainActivity.java的源代码如下:
package com.example.myapplication2;
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 Activity implements View.OnClickListener{
    
    
    Button btn;
    EditText edit;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit=(EditText)findViewById(R.id.edit);
        btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(this);
    }

    public void onClick(View view) {
    
    
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("edit", edit.getText().toString());
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
5、控制文件SecondActivity.java的源代码如下:
package com.example.myapplication2;
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 Activity implements View.OnClickListener{
    
    
    Button btn;
    EditText edit;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit=(EditText)findViewById(R.id.edit);
        btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(this);
    }

    public void onClick(View view) {
    
    
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("edit", edit.getText().toString());
        intent.putExtras(bundle);
        startActivity(intent);
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wkt1105436760/article/details/115408110