Android 开发笔记___Intent的使用

时间:2021-10-19 22:10:44
 public class ActRequestActivity extends AppCompatActivity implements OnClickListener {

     private EditText et_request;
private TextView tv_request; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_request);
findViewById(R.id.btn_act_request).setOnClickListener(this);
et_request = (EditText) findViewById(R.id.et_request);
tv_request = (TextView) findViewById(R.id.tv_request);
} @Override
public void onClick(View v) {
if (v.getId() == R.id.btn_act_request) {
Intent intent = new Intent();
intent.setClass(this, ActResponseActivity.class);
intent.putExtra("request_time", DateUtil.getNowTime());
intent.putExtra("request_content", et_request.getText().toString());
startActivityForResult(intent, 0);
}
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
String response_time = data.getStringExtra("response_time");
String response_content = data.getStringExtra("response_content");
String desc = String.format("收到返回消息:\n应答时间为%s\n应答内容为%s",
response_time, response_content);
tv_request.setText(desc);
}
} }
 public class ActResponseActivity extends AppCompatActivity implements OnClickListener {

     private EditText et_response;
private TextView tv_response; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_response);
findViewById(R.id.btn_act_response).setOnClickListener(this);
et_response = (EditText) findViewById(R.id.et_response);
tv_response = (TextView) 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_response.setText(desc);
} @Override
public void onClick(View v) {
if (v.getId() == R.id.btn_act_response) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("response_time", DateUtil.getNowTime());
bundle.putString("response_content", et_response.getText().toString());
intent.putExtras(bundle);
setResult(Activity.RESULT_OK, intent);
finish();
}
} }