第一个Activity
<TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_response" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="传送请求数据"/> <TextView android:id="@+id/tv_response" android:layout_width="match_parent" android:layout_height="wrap_content"/>
public class ActRequest8 extends AppCompatActivity implements View.OnClickListener { private TextView tv_result,tv_response; String request = "你吃饭了吗?"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act_request8); tv_response = findViewById(R.id.tv_response); tv_result = findViewById(R.id.tv_result); tv_result.setText("待发送的消息为:"+request); findViewById(R.id.btn_response).setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = new Intent(this,ActResponse8.class); //创建一个包裹 Bundle bundle = new Bundle(); bundle.putString("request_time","2023-4-10"); bundle.putString("request_content",request); intent.putExtras(bundle); startActivityForResult(intent,1); } // 第一个参数表示请求码;第二个参数表示回传码,第三个参数表示回传的数据 protected void onActivityResult(int requestCode, int resultCode, Intent intent) { // 接收返回数据 super.onActivityResult(requestCode, resultCode,intent); if (intent != null && requestCode == 1 && resultCode== Activity.RESULT_OK){ Bundle bundle = intent.getExtras(); String response_time = bundle.getString("response_time"); String response_content = bundle.getString("response_content"); String desc = String.format("收到请求消息:\n应答时间为:%s\n应答内容为:%s",response_time,response_content); tv_response.setText(desc); } } }
第二个Activity
<TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_response" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="返回应答数据"/> <TextView android:id="@+id/tv_response" android:layout_width="match_parent" android:layout_height="match_parent"/>
public class ActResponse8 extends AppCompatActivity implements View.OnClickListener { private TextView tv_result,tv_response; String response = "我吃过了"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act_response8); tv_result = findViewById(R.id.tv_result); tv_response = findViewById(R.id.tv_response); Bundle bundle = getIntent().getExtras(); String request_time = bundle.getString("request_time"); String request_content = bundle.getString("request_content"); String desc = String.format("收到请求消息:\n请求时间为:%s\n请求内容为:%s",request_time,request_content); tv_result.setText(desc); findViewById(R.id.btn_response).setOnClickListener(this); TextView tv_response = findViewById(R.id.tv_response); tv_response.setText("待返回的消息为:" + response); } @Override public void onClick(View v) { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("response_time","2023-4-11"); bundle.putString("response_content",response); intent.putExtras(bundle); // 携带意图返回上一个页面,RESULT_OK表示处理成功 setResult(Activity.RESULT_OK,intent); finish(); }