Android向下一个Activity传递参数

1.简单介绍

利用Intent我们可以从一个页面到另外一个页面(Activity)。这个过程中,我们可以利用Bundle包裹传递消息。Bundle存放数据的结构实际上是Map的映射,我们只需要用put方法把参数放进Bundle,然后再用Intent的putExtras方法把Bundle丢进Intent即可。接收页面接收参数时,先调用getIntent方法获取Intent,然后调用getExtras方法获取Bundle,最后就像使用Map一样取出参数即可。
上一个页面传递参数
下一个页面接收参数

2.简单实现

  1. 发送参数页面
  • activity_send_parameter.xml
<?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=".SendParameterActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="姓名"
        />

    <EditText
        android:inputType="number"
        android:maxLength="3"
        android:id="@+id/et_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="年龄"
        />

    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="11"
        android:hint="电话"/>

    <Button
        android:text="传递参数给下一个页面"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_ok"/>
</LinearLayout>
  • SendParameterActivity.java
package xyz.strasae.androidlearn.my;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class SendParameterActivity extends AppCompatActivity {
    private EditText et_name;
    private EditText et_phone;
    private EditText et_age;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_parameter);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_phone = findViewById(R.id.et_phone);
        findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SendParameterActivity.this, ReceiveParameterActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("name", et_name.getText().toString());
                bundle.putInt("age", Integer.valueOf(et_age.getText().toString()));
                bundle.putString("phone", et_phone.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
}
  1. 接收参数页面
  • activity_receive_parameter.xml
<?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=".ReceiveParameterActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_show"
        android:textColor="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="用于接收上一个页面传来的参数的TextView"
        />
</LinearLayout>
  • ReceiveParameterActivity.java
package xyz.strasae.androidlearn.my;

import androidx.appcompat.app.AppCompatActivity;

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

public class ReceiveParameterActivity extends AppCompatActivity {
    private TextView tv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_parameter);
        tv_show = findViewById(R.id.tv_show);
        Bundle bundle = this.getIntent().getExtras();
        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        int age = bundle.getInt("age");
        tv_show.setText(String.format("姓名:%s\n电话:%s\n年龄:%d", name, phone, age));
    }
}

3.更多

向上一个页面返回参数戳这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值