android之startActivityForResult的使用

2022-07-20 13:58:42 浏览数 (1)

大家好,又见面了,我是全栈君。

在Android中startActivityForResult主要作用就是:

A-Activity需要在B-Activtiy中执行一些数据操作,而B-Activity又要将,执行操作数据的结果返回给A-Activtiy

//看效果

//看代码

[java]

  1. package jason.actres;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. public class ActivityResultDemoActivity extends Activity
  10. {
  11. private Button button_start_task;
  12. private TextView TextView_result;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState)
  15. {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. button_start_task=(Button) findViewById(R.id.button_start_task);
  19. TextView_result=(TextView) findViewById(R.id.TextView_result);
  20. button_start_task.setOnClickListener(new button_start_task_Listener());
  21. }
  22. private class button_start_task_Listener implements OnClickListener
  23. {
  24. public void onClick(View v)
  25. {
  26. Intent intent=new Intent(ActivityResultDemoActivity.this,SimpleTaskActivity.class);
  27. //关键点来了,使用startActivityForResult来启动
  28. startActivityForResult(intent, 100);
  29. }
  30. }
  31. /**
  32. * 复写onActivityResult,这个方法
  33. * 是要等到SimpleTaskActivity点了提交过后才会执行的
  34. */
  35. @Override
  36. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  37. {
  38. //可以根据多个请求代码来作相应的操作
  39. if(20==resultCode)
  40. {
  41. String bookname=data.getExtras().getString(“bookname”);
  42. String booksale=data.getExtras().getString(“booksale”);
  43. TextView_result.setText(“书籍名称:” bookname “书籍价钱” booksale “元”);
  44. }
  45. super.onActivityResult(requestCode, resultCode, data);
  46. }
  47. }

//任务处理类

[java]

  1. package jason.actres;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. public class SimpleTaskActivity extends Activity
  10. {
  11. private EditText EditText_bookname;
  12. private EditText EditText_booksale;
  13. private Button Button_task_fulfill;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState)
  16. {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main2);
  19. EditText_bookname=(EditText) findViewById(R.id.EditText_bookname);
  20. EditText_booksale=(EditText) findViewById(R.id.EditText_booksale);
  21. Button_task_fulfill=(Button) findViewById(R.id.Button_task_fulfill);
  22. Button_task_fulfill.setOnClickListener(new Button_task_fulfill_Listener());
  23. }
  24. private class Button_task_fulfill_Listener implements OnClickListener
  25. {
  26. public void onClick(View v)
  27. {
  28. String str_bookname=EditText_bookname.getText().toString();
  29. String str_booksale=EditText_booksale.getText().toString();
  30. //判断空,我就不判断了。。。。
  31. Intent data=new Intent();
  32. data.putExtra(“bookname”, str_bookname);
  33. data.putExtra(“booksale”, str_booksale);
  34. //请求代码可以自己设置,这里设置成20
  35. setResult(20, data);
  36. //关闭掉这个Activity
  37. finish();
  38. }
  39. }
  40. }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/121439.html原文链接:https://javaforall.cn

0 人点赞