当前位置:   article > 正文

Android的startActivityForResult()用法_android startactivityforresult

android startactivityforresult

startActivityForResult()的作用

      相信很多人使用过startActivity(Intent intent)方法,调用该方法后,将会向目标activity跳转,但是该跳转只是单纯地跳转,并没有携带任何请求码。而startActivityForResult(Intent intent,int requestcode)的作用其实跟startActivity(Intent intent)相似,不同的是startActivityForResult()是带有请求码(请求码的作用主要是帮助activity判断来源)的跳转,跳转完成之后将会将requestcode传递给 onActivityResult(int requestCode,int resultCode,Intent data)。

startActivityForResult(Intent intent, int requestcode)

第一个参数Intent,和普通的startActivity()里的Intent一样,里面放要请求的Activity和可能需要放的数据。

第二个参数int,是一个请求代码,整型的,这个可以自己随便定义,但这个数要大于等于0才行。因为MainActivity有可能会跳转到多个页面,如果这几个页面使用完之后,都需要返回一些信息,那么就必须要有个标识符来表示返回来过的是哪个页面的信息。

setResult(int, Intent)

第一个参数int,是一个返回代码,整型的,这个也是自己随便定义,用来表示这个页面执行之后是个什么情况,是成功还是失败了,还是其它什么的,反正返回一个整型数,自己知道它的含义就行了。

第二个可选参数是一个Intent,可以用来存放数据

onActivityResult(int requestCode,int resultCode,Intent data)

这个方法是请求的Activity完成任务被finish()之后,会调用这个,前提是,你启动那个Activity是通过startActivityForResult()来启动的。

第一个参数int,是请求代码,就是startActivityForResult()里面的请求代码。

第二个参数int,是返回代码,就是setResult()方法里面的设置的参数。

第三个参数Intent,就是setResult(int, Intent)里面的放置数据的Intent。

下面看一段示例

     mainactivity的java代码:

  1. package com.example.returnrusltactivity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. public class MainActivity extends Activity {
  12. final int CODE=0x11; //定义一个校验码
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. Button button=(Button)findViewById(R.id.loginButton);
  18. button.setOnClickListener(new OnClickListener(){
  19. @Override
  20. public void onClick(View v) {
  21. Intent intent=new Intent();
  22. EditText l1=(EditText)findViewById(R.id.maineditview1);
  23. EditText l2=(EditText)findViewById(R.id.maineditview2);
  24. Bundle bundle=new Bundle();
  25. bundle.putCharSequence("user_name", l1.getText().toString()); //将两个编辑框的内容读取出来保存到bundle中
  26. bundle.putCharSequence("password_name", l2.getText().toString());
  27. intent.setClass(MainActivity.this,secondActivity.class );
  28. intent.putExtras(bundle);
  29. startActivityForResult(intent, 0x11); //跳转
  30. }
  31. });
  32. }
  33. @Override
  34. protected void onActivityResult(int requestCode,int resultCode,Intent data) {
  35. //super.onActivityResult(resultCode, requestCode, data);
  36. if(requestCode==CODE && resultCode==CODE ) { //判断requestCode和resultCode是否符合条件
  37. EditText e1=(EditText)findViewById(R.id.maineditview1);
  38. EditText e2=(EditText)findViewById(R.id.maineditview2);
  39. //Intent intent=getIntent();
  40. Bundle bundle=data.getExtras();
  41. e1.setText(bundle.getString("user")); //将第二个activity保存的数据填写到两个编辑框中
  42. e2.setText(bundle.getString("password"));
  43. }
  44. }
  45. @Override
  46. public boolean onCreateOptionsMenu(Menu menu) {
  47. // Inflate the menu; this adds items to the action bar if it is present.
  48. getMenuInflater().inflate(R.menu.main, menu);
  49. return true;
  50. }
  51. @Override
  52. public boolean onOptionsItemSelected(MenuItem item) {
  53. // Handle action bar item clicks here. The action bar will
  54. // automatically handle clicks on the Home/Up button, so long
  55. // as you specify a parent activity in AndroidManifest.xml.
  56. int id = item.getItemId();
  57. if (id == R.id.action_settings) {
  58. return true;
  59. }
  60. return super.onOptionsItemSelected(item);
  61. }
  62. }

secondActivity的Java代码:

  1. package com.example.returnrusltactivity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. public class secondActivity extends Activity {
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.second_activity);
  14. Button r=(Button)findViewById(R.id.secondReturnButton);
  15. final EditText e1=(EditText)findViewById(R.id.secondeditview1);
  16. final EditText e2=(EditText)findViewById(R.id.secondeditview2);
  17. Bundle bundle=new Bundle();
  18. Intent i=getIntent();
  19. bundle=i.getExtras();
  20. String us=bundle.getString("user_name");
  21. String ps=bundle.getString("password_name");
  22. e1.setText(us); //将主activity编辑框的内容自动填写到该activity中
  23. e2.setText(ps);
  24. r.setOnClickListener(new Button.OnClickListener() {
  25. public void onClick(View v) {
  26. Intent intent=new Intent();
  27. Bundle bundle=new Bundle();
  28. Intent i=getIntent();
  29. bundle=i.getExtras();
  30. String us=bundle.getString("user_name");
  31. String ps=bundle.getString("password_name");
  32. us=Integer.toString((Integer.parseInt(us))+1); //对主activity传输过来的内容进行处理,然后传递回主activity
  33. ps=Integer.toString((Integer.parseInt(ps))+2);
  34. bundle.putCharSequence("user", us);
  35. bundle.putCharSequence("password",ps);
  36. intent.putExtras(bundle);
  37. setResult(0x11,intent);
  38. finish();
  39. }
  40. });
  41. }
  42. @Override
  43. public boolean onCreateOptionsMenu(Menu menu) {
  44. // Inflate the menu; this adds items to the action bar if it is present.
  45. getMenuInflater().inflate(R.menu.main, menu);
  46. return true;
  47. }
  48. @Override
  49. public boolean onOptionsItemSelected(MenuItem item) {
  50. // Handle action bar item clicks here. The action bar will
  51. // automatically handle clicks on the Home/Up button, so long
  52. // as you specify a parent activity in AndroidManifest.xml.
  53. int id = item.getItemId();
  54. if (id == R.id.action_settings) {
  55. return true;
  56. }
  57. return super.onOptionsItemSelected(item);
  58. }
  59. }

main的配置文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="com.example.returnrusltactivity.MainActivity"
  11. android:orientation="vertical"
  12. android:gravity="center_vertical">
  13. <EditText android:id="@+id/maineditview1"
  14. android:layout_height="wrap_content"
  15. android:layout_width="match_parent"
  16. android:hint="@string/user"
  17. />
  18. "
  19. <EditText android:id="@+id/maineditview2"
  20. android:layout_height="wrap_content"
  21. android:layout_width="match_parent"
  22. android:hint="@string/password"
  23. />
  24. <Button android:id="@+id/loginButton"
  25. android:layout_height="wrap_content"
  26. android:layout_width="match_parent"
  27. android:text="@string/login"/>"
  28. </LinearLayout>

secondactivity的配置文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:stretchColumns="0,3"
  6. android:configChanges="orientation|screenSize|keyboardHidden">
  7. <TableRow
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" >
  10. <Button
  11. android:id="@+id/secondReturnButton"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/back"
  15. android:background="#00000000"
  16. android:gravity="top|start"
  17. />
  18. </TableRow>
  19. <TableRow
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent"
  22. android:gravity="center_vertical"
  23. android:id="@+id/secondTable1"
  24. android:layout_weight="1">
  25. <LinearLayout
  26. android:id="@+id/secondLinear1"
  27. android:layout_width="match_parent"
  28. android:layout_height="match_parent"
  29. android:gravity="center_vertical"
  30. android:orientation="vertical" >
  31. <EditText android:id="@+id/secondeditview1"
  32. android:layout_height="wrap_content"
  33. android:layout_width="match_parent"
  34. android:hint="@string/user"/>
  35. <EditText android:id="@+id/secondeditview2"
  36. android:layout_height="wrap_content"
  37. android:layout_width="match_parent"
  38. android:hint="@string/password"/>
  39. <Button android:id="@+id/registerButton"
  40. android:layout_height="wrap_content"
  41. android:layout_width="match_parent"
  42. android:text="@string/register"/>
  43. <TextView
  44. android:layout_width="wrap_content"
  45. android:layout_height="50dp"/>
  46. </LinearLayout>
  47. </TableRow>
  48. </TableLayout>

程序演示:



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/886506
推荐阅读
相关标签
  

闽ICP备14008679号