Android------startActivityForResult的详细用法

来源:互联网 发布:淘宝上最火的男装店铺 编辑:程序博客网 时间:2024/05/01 01:38

在启动另外一个Activity的时候,有两种方法,一种是直接使用startActivity,另外一种就是使用startActivityForResult。前一种想必大家都明白怎么使用了,我就不废话了。本文主要通过一个Demo来学习一下第二种。

startActivityForResult的主要作用就是它可以回传数据,假设我们有两个页面,首先进入第一个页面,里面有一个按钮,用于进入下一个页面,当进入下一个页面时,进行设置操作,并在其finish()动作或者back动作后,将设置的值回传给第一个页面,从而第一个页面来显示所得到的值。这个有一点像回调方法,就是在第二个页面finish()动作或者back动作后,会回调第一个页面的onActivityResult()方法,所以我们可以重写一下这个方法。直接看代码吧:
第一个页面代码:

[java] view plaincopy
  1. package org.sunchao;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10.   
  11. public class TestStartActivityForResultActivity extends Activity implements  
  12.         OnClickListener {  
  13.     private TextView mText01;  
  14.     private TextView mText02;  
  15.     private Button button01;  
  16.     private Button button02;  
  17.     private Intent mIntent;  
  18.     private int requestCode;  
  19.   
  20.     /** Called when the activity is first created. */  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         mText01 = (TextView) findViewById(R.id.mText01);  
  26.         mText02 = (TextView) findViewById(R.id.mText02);  
  27.         button01 = (Button) findViewById(R.id.mButton01);  
  28.         button02 = (Button) findViewById(R.id.mButton02);  
  29.         button01.setOnClickListener(this);  
  30.         button02.setOnClickListener(this);  
  31.         mText01.setText("01");  
  32.         mText02.setText("02");  
  33.   
  34.         mIntent = new Intent();  
  35.         mIntent.setClass(TestStartActivityForResultActivity.this,  
  36.                 Activity02.class);  
  37.     }  
  38.   
  39.     @Override  
  40.     public void onClick(View v) {  
  41.         switch (v.getId()) {  
  42.         case R.id.mButton01:  
  43.             // 请求码的值随便设置,但必须>=0  
  44.             requestCode = 0;  
  45.             startActivityForResult(mIntent, requestCode);  
  46.             break;  
  47.         case R.id.mButton02:  
  48.             requestCode = 2;  
  49.             startActivityForResult(mIntent, requestCode);  
  50.             break;  
  51.         default:  
  52.             break;  
  53.         }  
  54.     }  
  55.   
  56.     // 回调方法,从第二个页面回来的时候会执行这个方法  
  57.     @Override  
  58.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  59.         String change01 = data.getStringExtra("change01");  
  60.         String change02 = data.getStringExtra("change02");  
  61.         // 根据上面发送过去的请求吗来区别  
  62.         switch (requestCode) {  
  63.         case 0:  
  64.             mText01.setText(change01);  
  65.             break;  
  66.         case 2:  
  67.             mText02.setText(change02);  
  68.             break;  
  69.         default:  
  70.             break;  
  71.         }  
  72.     }  
  73. }  
第一个页面布局文件:
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:id="@+id/mText01"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     />  
  12. <TextView    
  13.     android:id="@+id/mText02"  
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content"   
  16.     />  
  17. <Button   
  18.     android:id="@+id/mButton01"  
  19.     android:layout_width="fill_parent"   
  20.     android:layout_height="wrap_content"  
  21.     android:text="改变第一行文本的值"  
  22.     />  
  23. <Button   
  24.     android:id="@+id/mButton02"  
  25.     android:layout_width="fill_parent"   
  26.     android:layout_height="wrap_content"  
  27.     android:text="改变第二行文本的值"  
  28.     />  
  29. </LinearLayout>  
第二个页面代码:
[java] view plaincopy
  1. package org.sunchao;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6.   
  7. public class Activity02 extends Activity {  
  8.     private int resultCode = 0;  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity02);  
  14.         Intent mIntent = new Intent();  
  15.         mIntent.putExtra("change01""1000");  
  16.         mIntent.putExtra("change02""2000");  
  17.         // 设置结果,并进行传送  
  18.         this.setResult(resultCode, mIntent);  
  19.         this.finish();  
  20.     }  
  21.   
  22. }  
第二个页面布局文件:
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="文本的值已经改变"  
  11.     />  
  12. </LinearLayout>  
AndroidManifest.xml:
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="org.sunchao"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".TestStartActivityForResultActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.         <activity android:name=".Activity02" />  
  17.   
  18.     </application>  
  19. </manifest>  
运行效果图:






代码下载地址:http://download.csdn.net/source/3448804

原文地址:点击打开链接

原创粉丝点击