android 打开新活动并返回结果

来源:互联网 发布:大数据专业有哪些 编辑:程序博客网 时间:2024/05/16 11:37

android startActivityForResult(Intent intent, int requestCode) 整理与总结!

搞了4个月android一直没用过startActivityForResult,这突然用了一下还真有点懵,查看API并google了一下,特整理如下:

假设有两个Activity,主界面A,功能界面B,由A启动B,并传数据给B,B在经过处理后把数据传回给A。

先是A传B:

view plaincopy to clipboardprint?
  1. Bundle bundle = new Bundle();  
  2. bundle.putString("Dir""/sdcard");  
  3. Intent intent=new Intent();  
  4. intent.putExtras(bundle);  
  5. intent.setClass(A.this,B.class);  
  6. A.this.startActivityForResult(intent,0);  
  7. //这里的0代表requestCode,就是用来做个标记(要求是大于等于0的整数);  
 


 然后就是B接收再传回:

view plaincopy to clipboardprint?
  1. Intent it = new Intent();   
  2. Bundle bundle=it.getExtras();  
  3. String mString=bundle.getString("Dir");  
  4.   
  5. mString=mString+"/"  
  6. bundle.putString("Dir",mString);  
  7. B.this.setResult(0, it);//0与前面A里的0对应  
  8. finish();   


A最后再接收B回传的结果:

 

view plaincopy to clipboardprint?
  1. protected void onActivityResult(int requestCode, int resultCode, Intent data)  
  2.      {  
  3.      //B返回时触发  
  4.    
  5.      }   
 

 

最后以一个SDK开发大全上面的例子来加深理解

先是A传入B,并且把接收B传回结果的接收器写在A中

view plaincopy to clipboardprint?
  1. package com.my;  
  2.   
  3. /* import相关class */  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.RadioButton;  
  11.   
  12. public class A extends Activity   
  13. {  
  14.   private EditText et;  
  15.   private RadioButton rb1;  
  16.   private RadioButton rb2;  
  17.       
  18.   /** Called when the activity is first created. */  
  19.   @Override  
  20.   public void onCreate(Bundle savedInstanceState)   
  21.   {  
  22.     super.onCreate(savedInstanceState);  
  23.     /* 载入main.xml Layout */  
  24.     setContentView(R.layout.main);  
  25.       
  26.     /* 以findViewById()取得Button对象,并添加onClickListener */  
  27.     Button b1 = (Button) findViewById(R.id.button1);  
  28.     b1.setOnClickListener(new Button.OnClickListener()  
  29.     {  
  30.       public void onClick(View v)  
  31.       {  
  32.         /*取得输入的身高*/  
  33.         et = (EditText) findViewById(R.id.height);  
  34.         double height=Double.parseDouble(et.getText().toString());  
  35.         /*取得选择的性别*/  
  36.         String sex="";  
  37.         rb1 = (RadioButton) findViewById(R.id.sex1);  
  38.         rb2 = (RadioButton) findViewById(R.id.sex2);  
  39.         if(rb1.isChecked())  
  40.         {  
  41.           sex="M";  
  42.         }  
  43.         else  
  44.         {  
  45.           sex="F";  
  46.         }      
  47.           
  48.         /*new一个Intent对象,并指定class*/  
  49.         Intent intent = new Intent();  
  50.         intent.setClass(A.this,B.class);  
  51.           
  52.         /*new一个Bundle对象,并将要传递的数据传入*/  
  53.         Bundle bundle = new Bundle();  
  54.         bundle.putDouble("height",height);  
  55.         bundle.putString("sex",sex);  
  56.         
  57.         /*将Bundle对象assign给Intent*/  
  58.         intent.putExtras(bundle);  
  59.         
  60.         /*调用Activity B*/  
  61.         startActivityForResult(intent,0);  
  62.       }  
  63.     });  
  64.   }  
  65.     
  66.   /* 覆盖 onActivityResult()*/  
  67.   @Override  
  68.   protected void onActivityResult(int requestCode, int resultCode,  
  69.                                   Intent data)  
  70.   {  
  71.     switch (resultCode)  
  72.     {   
  73.       case 0:  
  74.         /* 取得来自Activity2的数据,并显示于画面上 */    
  75.         Bundle bunde = data.getExtras();  
  76.         String sex = bunde.getString("sex");  
  77.         double height = bunde.getDouble("height");  
  78.           
  79.         et.setText(""+height);  
  80.         if(sex.equals("M"))  
  81.         {  
  82.           rb1.setChecked(true);  
  83.         }  
  84.         else  
  85.         {  
  86.           rb2.setChecked(true);  
  87.         }  
  88.         break;  
  89.       default:  
  90.         break;  
  91.      }   
  92.    }   
  93. }  

然后是B接收到A,再回传给A

view plaincopy to clipboardprint?
  1. package com.my;  
  2.   
  3. /* import相关class */  
  4. import java.text.DecimalFormat;  
  5. import java.text.NumberFormat;  
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.TextView;  
  12.   
  13. public class B extends Activity   
  14. {  
  15.   Bundle bunde;  
  16.   Intent intent;  
  17.   /** Called when the activity is first created. */  
  18.   @Override  
  19.   public void onCreate(Bundle savedInstanceState)   
  20.   {  
  21.     super.onCreate(savedInstanceState);  
  22.     /* 载入mylayout.xml Layout */  
  23.     setContentView(R.layout.myalyout);  
  24.       
  25.     /* 取得Intent中的Bundle对象 */  
  26.     intent=this.getIntent();  
  27.     bunde = intent.getExtras();  
  28.       
  29.     /* 取得Bundle对象中的数据 */  
  30.     String sex = bunde.getString("sex");  
  31.     double height = bunde.getDouble("height");  
  32.       
  33.     /* 判断性别 */  
  34.     String sexText="";  
  35.     if(sex.equals("M"))  
  36.     {  
  37.       sexText="男性";  
  38.     }  
  39.     else  
  40.     {  
  41.       sexText="女性";  
  42.     }  
  43.       
  44.     /* 取得标准体重 */  
  45.     String weight=this.getWeight(sex, height);  
  46.       
  47.     /* 设置输出文字 */  
  48.     TextView tv1=(TextView) findViewById(R.id.text1);  
  49.     tv1.setText("你是一位"+sexText+"/n你的身高是"+height+  
  50.                    "厘米/n你的标准体重是"+weight+"公斤");  
  51.       
  52.     /* 以findViewById()取得Button对象,并添加onClickListener */  
  53.     Button b1 = (Button) findViewById(R.id.button1);  
  54.     b1.setOnClickListener(new Button.OnClickListener()  
  55.     {  
  56.       public void onClick(View v)  
  57.       {            
  58.         /* 返回result回上一个activity */  
  59.         B.this.setResult(0, intent);  
  60.           
  61.         /* 结束这个activity */  
  62.         B.this.finish();  
  63.       }  
  64.     });  
  65.   }  
  66.     
  67.   /* 四舍五入的method */  
  68.   private String format(double num)  
  69.   {  
  70.     NumberFormat formatter = new DecimalFormat("0.00");  
  71.     String s=formatter.format(num);  
  72.     return s;  
  73.   }  
  74.   
  75.   /* 以findViewById()取得Button对象,并添加onClickListener */    
  76.   private String getWeight(String sex,double height)  
  77.   {  
  78.     String weight="";  
  79.     if(sex.equals("M"))  
  80.     {  
  81.       weight=format((height-80)*0.7);  
  82.     }  
  83.     else  
  84.     {  
  85.       weight=format((height-70)*0.6);  
  86.     }    
  87.     return weight;  
  88.   }  
  89. }  

都结束了,就那么多东西,回家打dota去了,哈哈,我的vs号是Hello___Wo__,谁有空加我一起搞啊,哈哈哈!

原创粉丝点击