技术资料 >> 手机应用开发 |
|
|
搜索标签:
onActivityResult
|
onActivityResult方法使用 |
[阅读次数:727次] [发布时间:2013年6月13日] |
主要的Activity,一会会先执行此Activity,然后调用B Activity,并向其发送数据,通过回调函数来获取B传回来的值。 public class onActivityResult extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); StartA(); } public void StartA() { Button buttonA = (Button)findViewById(R.id.button1); buttonA.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub // onActivityResult.this.setContentView(R.layout.second); // onActivityResult.this.StartB(); Intent intent = new Intent(onActivityResult.this,ClassB.class); String passString = "Hello,this is A"; intent.putExtra("ToB", passString); startActivityForResult(intent, 0); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { //resultCode为回传的标记,我在B中回传的是RESULT_OK case RESULT_OK: Bundle b=data.getExtras(); //data为B中回传的Intent String str=b.getString("FromB");//str即为回传的值 TextView view = (TextView)findViewById(R.id.textA); view.setText(str); break; default: break; } } /*public void StartB() { Button buttonB = (Button)findViewById(R.id.button2); buttonB.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub onActivityResult.this.setContentView(R.layout.main); onActivityResult.this.StartA(); } }); }*/ } ---------------------------------------------------------------------------------------------------------------------- ClassB.java public class ClassB extends Activity{ private Intent intent; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); intent = getIntent(); String result = intent.getStringExtra("ToB"); Button button = (Button)findViewById(R.id.button2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(ClassB.this,onActivityResult.class); String passString = "Hello,this is B"; intent.putExtra("FromB", passString); setResult(RESULT_OK, intent); finish(); } }); TextView textView = (TextView)findViewById(R.id.textB); textView.setText(result); } }
本页地址:
[复制地址]
该页内容非本站原创 收藏自:http://blog.csdn.net/xu_song/article/details/7711043
|
返回顶部 |
|
|
|
|
推荐链接 |
|
最近更新 |
|
|
|
热门浏览 |
|
|
|
|