码迷,mamicode.com
首页 > 其他好文 > 详细

获取 Activity 的返回参数

时间:2015-12-15 21:18:12      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

第一个Activity

 

public class MainActivity extends ActionBarActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.startActy);
tv=(TextView) findViewById(R.id.textView);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,TheAty.class);
// Bundle bundle = new Bundle();
// bundle.putString("name", "王城");
// bundle.putInt("age", 26);
//i.putExtras(bundle);
i.putExtra("data", new User("wangcheng",25));
startActivityForResult(i, 0);;
}
});

}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
tv.setText(data.getData().toString());
};

第二个Activity

public class TheAty extends ActionBarActivity {
EditText ed;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_aty);
TextView tv = (TextView) findViewById(R.id.otherActy);
ed = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.btn);
Intent i = getIntent();
//Bundle data = i.getExtras();
//Bundle data = i.getBundleExtra("data");
//User user=(User) i.getSerializableExtra("data")
User user=(User) i.getParcelableExtra("data");

tv.setText(String.format("User info(name=%s,age=%d)",user.getName(),user.getAge()));
//tv.setText(data.getString("name"));
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String uriString = ed.getText().toString();
Uri data = Uri.parse(uriString);
Intent intent = new Intent(null, data);
setResult(0,intent);
finish();
}
});
}

 

获取 Activity 的返回参数

标签:

原文地址:http://www.cnblogs.com/cityking/p/a009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!