1.简单介绍
利用Intent我们可以从一个页面到另外一个页面(Activity)。这个过程中,我们可以利用Bundle包裹传递消息。Bundle存放数据的结构实际上是Map的映射,我们只需要用put方法把参数放进Bundle,然后再用Intent的putExtras方法把Bundle丢进Intent即可。接收页面接收参数时,先调用getIntent方法获取Intent,然后调用getExtras方法获取Bundle,最后就像使用Map一样取出参数即可。
2.简单实现
- 发送参数页面
- 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);
}
});
}
}
- 接收参数页面
- 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.更多
向上一个页面返回参数戳这里。