

Intent对象的setData方法只指定到达目标的路径,不能携带参数信息。真正的参数存在Extras,我么可以直接使用setExtra方法,传递包括整型、浮点型、字符串等基本数据类型,甚至还包括Serializable这样的序列化结构。但是这样调用显然数据随便丢进去不好管理,所以Android引入了Bundle概念。

XML代码:
- //主界面
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center"
- tools:context=".MainActivity">
-
- <EditText
- android:id="@+id/message"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="你想说的话:"
- />
- <Button
- android:id="@+id/next"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="发送"
- />
- </LinearLayout>
-
- //界面二
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".Main2Activity">
- <TextView
- android:id="@+id/two"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
Java代码:
- //主界面
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import com.example.helloworld.util.DateUtil;
-
-
- public class MainActivity extends AppCompatActivity{
- EditText e;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- e=findViewById(R.id.message);
- findViewById(R.id.next).setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v) {
- String date=DateUtil.getNowTime(); //获取当前时间
- String message=e.getText().toString(); //获取编辑框内信息
- Bundle bundle=new Bundle();
- bundle.putString("date", date); //将时间信息放在Bundle中,key为date
- bundle.putString("message", message); //将编辑框信息放在Bundle中,key为message
- Intent intent=new Intent(MainActivity.this,Main2Activity.class);
- intent.putExtras(bundle); //将Bundle添加到意图中
- startActivity(intent); //跳转到意图指定活动界面
- }
- });
- }
- }
- //界面二
- import android.os.Bundle;
- import android.widget.TextView;
- import androidx.appcompat.app.AppCompatActivity;
-
- public class Main2Activity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- TextView t=findViewById(R.id.two);
- Bundle b= getIntent().getExtras(); //从上一个页面传来的意图中获取Bundle包裹
- String request_date=b.getString("date"); //从包裹中取出名为date的字符串
- String message=b.getString("message"); //从包裹中取出名为message的字符串
- t.setText(String.format("请求时间为:%s\n发送内容为:%s", request_date,message));
- }
- }
- //主界面
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center"
- tools:context=".MainActivity">
-
- <EditText
- android:id="@+id/message"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="你想说的话:"
- />
- <Button
- android:id="@+id/next"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="发送"
- />
-
- <TextView
- android:id="@+id/call_back_t"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textColor="#000000"
- />
- </LinearLayout>
-
- //界面二
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".Main2Activity">
- <TextView
- android:id="@+id/two"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textColor="#000000"
- />
- </LinearLayout>
Java代码:
- //主页面Java代码
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import com.example.helloworld.util.DateUtil;
-
-
- public class MainActivity extends AppCompatActivity{
- EditText editText;
- TextView call_back_t;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- editText =findViewById(R.id.message);
- call_back_t=findViewById(R.id.call_back_t);
- findViewById(R.id.next).setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v) {
- String date=DateUtil.getNowTime();
- String message= editText.getText().toString();
- Bundle bundle=new Bundle();
- bundle.putString("date", date);
- bundle.putString("message", message);
- Intent intent=new Intent(MainActivity.this,Main2Activity.class);
- intent.putExtras(bundle);
- //请求代码大于等于0则代表需要返回数据,请求代码表示每个跳转的唯一性,请求代码<=65535
- startActivityForResult(intent,0);
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- //返回数据data不为空 请求代码为0 结果代码为RESULT_OK 则代表返回成功
- if(data!=null&&requestCode==0&&resultCode== Activity.RESULT_OK)
- {
- Bundle bundle=data.getExtras();
- String message="";
- if(bundle.containsKey("receive_date"))
- message=bundle.getString("receive_date");
- call_back_t.setText("数据返回时间:"+message);
- }
-
- }
- }
-
- //页面二Java代码
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
- import androidx.appcompat.app.AppCompatActivity;
- import com.example.helloworld.util.DateUtil;
-
- public class Main2Activity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- TextView t=findViewById(R.id.two);
- Bundle b= getIntent().getExtras(); //打开上个页面传来的包裹
- String request_date=b.getString("date"); //取出名为date的字符串
- String message=b.getString("message"); //取出名为message的字符串
- //将信息显示到文本框内
- t.setText(String.format("请求时间为:%s\n发送内容为:%s", request_date,message));
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- //准备返回数据
- String receive_date = DateUtil.getNowTime();
- Intent intent = new Intent();
- intent.putExtra("receive_date", receive_date);
- setResult(Activity.RESULT_OK, intent);
- }
- }