快捷搜索:  汽车  科技

intent如何实现通信(每日一练之Intent发送数据)

intent如何实现通信(每日一练之Intent发送数据)| 字符串 | getString | putString || 布尔值 | getBoolean | putBoolean || 整数型 | getInt | putInt || 浮点数 | getFloat | putFloat || 双精度数 | getDouble | putDouble |

向下一个Activity发送数据

intent如何实现通信(每日一练之Intent发送数据)(1)

intent使用Bundle对象存放待传递的数据信息

| 数据类型 | 读方法 | 写方法 |

| ------------ | ------------ | ------------ |

| 整数型 | getInt | putInt |

| 浮点数 | getFloat | putFloat |

| 双精度数 | getDouble | putDouble |

| 布尔值 | getBoolean | putBoolean |

| 字符串 | getString | putString |

| 字符串数组 | getStringArray | getStringArray |

| 字符串列表 | getStringList | putStringList |

| 可序列化结构 | getSerializable | putSerializable |

发送页面

private TextView tv_send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intent_send); tv_send = findViewById(R.id.tv_send); Button btn_send = findViewById(R.id.btn_send); btn_send.setOnClickListener(handler); } private View.OnClickListener handler = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_send: //do something Intent intent = new Intent(); intent.setClass(IntentSendActivity.this IntentReceiveMainActivity.class); Bundle bundle = new Bundle(); bundle.putString("request_time" Utils.getNowTime()); bundle.putString("request_content" tv_send.getText().toString()); intent.putExtras(bundle); startActivity(intent); break; } } };

接收页面

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intent_receive_main); tv_receive = findViewById(R.id.tv_receive); Intent intent = new Intent(); 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_receive.setText(desc); Button btn_from_receive_to_send = findViewById(R.id.btn_from_receive_to_send); btn_from_receive_to_send.setOnClickListener(clickFn); } ```

猜您喜欢: