打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
onActivityResult传值的使用

有时候在群里加入的新人总会喜欢问一些过去的问题  有时候不想回答 是因为回答的次数多了

不回答又打击人的积极性  谁让自己接触的早呢  为了省劲还是把简单的东西作为指导篇吧

 

多个activity之间的传值 其实就是onActivityResult,然后别忘了还有一个action的问题 就是在主xml中添加自己的action以便于识别,最后次activity别忘了finansh。

Java代码  
  1. public class Wizard extends Activity {  
  2.   
  3.     private TextView step1result, step2result, step3result;  
  4.   
  5.     public static final String INTENT_STEP1 = "com.novoda.STEP1";  
  6.     public static final String INTENT_STEP2 = "com.novoda.STEP2";  
  7.     public static final String INTENT_STEP3 = "com.novoda.STEP3";  
  8.   
  9.     private static final int STEP1 = 1;  
  10.     private static final int STEP2 = 2;  
  11.     private static final int STEP3 = 3;  
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.wizard);  
  17.           
  18.         this.step1result = (TextView)findViewById(R.id.step1result);  
  19.         this.step2result = (TextView)findViewById(R.id.step2result);  
  20.         this.step3result = (TextView)findViewById(R.id.step3result);    
  21.           
  22.         startActivityForResult(new Intent(Wizard.INTENT_STEP1), STEP1);          
  23.     }  
  24.       
  25.       
  26.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  27.         switch (requestCode) {  
  28.             case STEP1:  
  29.                 this.step1result.setText(data.getStringExtra("STEP1RESULT"));  
  30.                 startActivityForResult(new Intent(Wizard.INTENT_STEP2), STEP2);      
  31.                 break;  
  32.             case STEP2:  
  33.                 this.step2result.setText(data.getStringExtra("STEP2RESULT"));  
  34.                 startActivityForResult(new Intent(Wizard.INTENT_STEP3), STEP3);      
  35.                 break;  
  36.             case STEP3:  
  37.                 this.step3result.setText(data.getStringExtra("STEP3RESULT"));  
  38.                 break;  
  39.         }  
  40.     }  
  41. }  

 

Java代码  
  1. public class Step1 extends Activity {  
  2.   
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.step1);  
  7.   
  8.         Button nextStep = (Button)findViewById(R.id.goto2);  
  9.         nextStep.setOnClickListener(new View.OnClickListener() {  
  10.             public void onClick(View v) {  
  11.                 Intent it = new Intent();  
  12.                 it.putExtra("STEP1RESULT", ((EditText)findViewById(R.id.step1value)).getText()  
  13.                         .toString());  
  14.                 setResult(Activity.RESULT_OK, it);  
  15.                 finish();  
  16.             }  
  17.         });  
  18.     }  
  19. }  

 

后面的step2 step3都是一样的了

然后还有主xml

Java代码  
  1. <application android:icon="@drawable/icon" android:label="@string/app_name">  
  2.         <activity android:name=".Wizard" android:label="@string/app_name">  
  3.             <intent-filter>  
  4.                 <action android:name="android.intent.action.MAIN" />  
  5.                 <category android:name="android.intent.category.LAUNCHER" />  
  6.             </intent-filter>  
  7.         </activity>  
  8.   
  9.         <activity android:name=".Step1" android:label="Step1">  
  10.             <intent-filter>  
  11.                 <action android:name="com.novoda.STEP1" />  
  12.                 <category android:name="android.intent.category.DEFAULT" />  
  13.             </intent-filter>  
  14.         </activity>  
  15.   
  16.         <activity android:name=".Step2" android:label="Step2">  
  17.             <intent-filter>  
  18.                 <action android:name="com.novoda.STEP2" />  
  19.                 <category android:name="android.intent.category.DEFAULT" />  
  20.             </intent-filter>  
  21.         </activity>  
  22.   
  23.         <activity android:name=".Step3" android:label="Step3">  
  24.             <intent-filter>  
  25.                 <action android:name="com.novoda.STEP3" />  
  26.                 <category android:name="android.intent.category.DEFAULT" />  
  27.             </intent-filter>  
  28.         </activity>  
  29.     </application>  
  30.     <uses-sdk android:minSdkVersion="7" />  
  31. </manifest>   

 

分享到:
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
1.Intent
一步一步android(12):关于Intent【接前一个例子】
加法计算器
startActivity的2种用法
系出名门Android(4) - 活动(Activity), 服务(Service), 广...
Android 程序重启 Activity 的方法资料
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服