首页 > 代码库 > Android 开发笔记___Intent的使用

Android 开发笔记___Intent的使用

 1 public class ActRequestActivity extends AppCompatActivity implements OnClickListener {
 2 
 3     private EditText et_request;
 4     private TextView tv_request;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_act_request);
10         findViewById(R.id.btn_act_request).setOnClickListener(this);
11         et_request = (EditText) findViewById(R.id.et_request);
12         tv_request = (TextView) findViewById(R.id.tv_request);
13     }
14 
15     @Override
16     public void onClick(View v) {
17         if (v.getId() == R.id.btn_act_request) {
18             Intent intent = new Intent();
19             intent.setClass(this, ActResponseActivity.class);
20             intent.putExtra("request_time", DateUtil.getNowTime());
21             intent.putExtra("request_content", et_request.getText().toString());
22             startActivityForResult(intent, 0);
23         }
24     }
25 
26     @Override
27     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
28         if (data != null) {
29             String response_time = data.getStringExtra("response_time");
30             String response_content = data.getStringExtra("response_content");
31             String desc = String.format("收到返回消息:\n应答时间为%s\n应答内容为%s",
32                     response_time, response_content);
33             tv_request.setText(desc);
34         }
35     }
36 
37 }

 

 1 public class ActResponseActivity extends AppCompatActivity implements OnClickListener {
 2 
 3     private EditText et_response;
 4     private TextView tv_response;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_act_response);
10         findViewById(R.id.btn_act_response).setOnClickListener(this);
11         et_response = (EditText) findViewById(R.id.et_response);
12         tv_response = (TextView) findViewById(R.id.tv_response);
13         
14         Bundle bundle = getIntent().getExtras();
15         String request_time = bundle.getString("request_time");
16         String request_content = bundle.getString("request_content");
17         String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s",
18                 request_time, request_content);
19         tv_response.setText(desc);
20     }
21 
22     @Override
23     public void onClick(View v) {
24         if (v.getId() == R.id.btn_act_response) {
25             Intent intent = new Intent();
26             Bundle bundle = new Bundle();
27             bundle.putString("response_time", DateUtil.getNowTime());
28             bundle.putString("response_content", et_response.getText().toString());
29             intent.putExtras(bundle);
30             setResult(Activity.RESULT_OK, intent);
31             finish();
32         }
33     }
34 
35 }

 

Android 开发笔记___Intent的使用