Activity间传递参数
不带回调方法(显示意图)
采用键值的方式:
发送数据:
public void btn_open_click(View v) {
EditText edtName = (EditText)findViewById(R.id.edt_name);
edtName.setText("张三");
Intent intSend = new Intent();
Log.i("test","ok1");
Bundle bundleSend = new Bundle();
Log.i("test","ok2");
bundleSend.putString("name","张三");
bundleSend.putString("age","20");
intSend.putExtras(bundleSend);
Log.i("test","ok3");
intSend.setClass(this, SecondActivity.class);
startActivity(intSend);
Log.i("test","ok4");
*********************************************************
接收数据
Intent intGet = getIntent();
Log.i("test","ok5");
Bundle bundleTemp = intGet.getExtras();
Log.i("test","ok6");
String strTemp;
strTemp = bundleTemp.getString("name");
Log.i("test","接收bundleTemp值成功");
TextView txtName = (TextView)findViewById(R.id.textName);
txtName.setText(strTemp);
采用传递对象参数的方式:
发送参数:
Intent intSend = new Intent();
Bundle bundleSend = new Bundle();
SendData toData = new SendData(); //传递参数的对象
toData.setAage(20);
toData.setName("李四");
Log.i("test","toData赋值成功");
bundleSend.putSerializable("toData", toData);
Log.i("test","bundleSend绑定对象成功");
intSend.putExtras(bundleSend);
Log.i("test","intSend绑定对象成功");
intSend.setClass(this, SecondActivity.class);
startActivity(intSend);
Log.i("test","发送参数成功");
接收参数:
Intent intGet = getIntent();
Bundle bundleTemp = intGet.getExtras();
Log.i("test", "intGet和bundleTemp初始化成功");
SendData getData = (SendData) bundleTemp.get("toData");
Log.i("test", "从bundleTemp中接收对象成功");
String strTemp;
strTemp = getData.getName();
Log.i("test","接收bundleTemp值成功");
TextView txtName = (TextView)findViewById(R.id.textName);
txtName.setText(strTemp);