首先,由于Activity是Android四大组件之一,如果一个应用程序中包含不止一个Activity,则需要在AndroidManifest.xml文件中进行声明。

例如进行如下的声明(程序中包含两个Activity):

 <activity android:name=".EX03_10"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><!-- 另一个Activity的声明 --></activity><activity android:name="EX03_10_1"></activity>

下面以两个例子说明不同的Activity之间传递数据的方法。

1.使用Bundle对象传递数据

传递数据使用的方法如下:

      /*new一个Intent对象,并指定class*/final Intent intent = new Intent(); intent.setClass(EX03_10.this,EX03_10_1.class); /*new一个Bundle对象,并将要传递的数据传入*/Bundle bundle = new Bundle();bundle.putDouble("height",height);bundle.putString("sex",sex); /*将Bundle对象assign给Intent*/ intent.putExtras(bundle); /*调用Activity EX03_10_1*/startActivity(intent); 

接收数据使用的方法如下:

   /* 取得Intent中的Bundle对象 */Bundle bunde = this.getIntent().getExtras();/* 取得Bundle对象中的数据 */String sex = bunde.getString("sex");double height = bunde.getDouble("height");

下面给出具体的实现代码:

1.1  主程序代码(第一个Activity)

public class EX03_10 extends Activity
{ /** Called when the activity is first created. */ @Overridepublic void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); /* 载入main.xml Layout */setContentView(R.layout.main); /* 以findViewById()取得Button对象,并加入onClickListener */ Button b1 = (Button) findViewById(R.id.button1);b1.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {/*取得输入的身高*/ EditText et = (EditText) findViewById(R.id.height);double height=Double.parseDouble(et.getText().toString()); /*取得选择的性别*/ String sex=""; RadioButton rb1 = (RadioButton) findViewById(R.id.sex1);if(rb1.isChecked()) { sex="M"; }else{sex="F";} /*new一个Intent对象,并指定class*/final Intent intent = new Intent(); intent.setClass(EX03_10.this,EX03_10_1.class); /*new一个Bundle对象,并将要传递的数据传入*/Bundle bundle = new Bundle();bundle.putDouble("height",height);bundle.putString("sex",sex); /*将Bundle对象assign给Intent*/ intent.putExtras(bundle); /*调用Activity EX03_10_1*/startActivity(intent); } }); }}

1.2 第二个Activity的实现代码:

public class EX03_10_1 extends Activity
{/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/* 加载main.xml Layout */setContentView(R.layout.myalyout);/* 取得Intent中的Bundle对象 */Bundle bunde = this.getIntent().getExtras();/* 取得Bundle对象中的数据 */String sex = bunde.getString("sex");double height = bunde.getDouble("height");/*判断性别 */String sexText="";if(sex.equals("M")){sexText="男性";}else{sexText="女性";}/* 取得标准体重 */String weight=this.getWeight(sex, height);/* 设定输出文字 */TextView tv1=(TextView) findViewById(R.id.text1);tv1.setText("你是一位"+sexText+"\n你的身高是"+height+"公分\n你的标准体重是"+weight+"公斤");}/* 四舍五入的method */private String format(double num){NumberFormat formatter = new DecimalFormat("0.00");String s=formatter.format(num);return s;}/* 以findViewById()取得Button对象,onClickListener */  private String getWeight(String sex,double height){String weight="";if(sex.equals("M")){weight=format((height-80)*0.7);}else{weight=format((height-70)*0.6);} return weight;}
}

2.返回数据到前一个Activity

这里需要使用startActivityForResult方法

传递数据的方法与上一个例子是类似的,实现的代码如下:

       /*new一个Intent对象,并指定class*/ Intent intent = new Intent(); intent.setClass(EX03_11.this,EX03_11_1.class);/*new一个Bundle对象,并将要传递的数据传入*/Bundle bundle = new Bundle(); bundle.putDouble("height",height);bundle.putString("sex",sex); /*将Bundle对象assign给Intent*/ intent.putExtras(bundle); /*调用Activity EX03_11_1*/ startActivityForResult(intent,0); 

注意:这里需要实现一个重载的方法:

protected void onActivityResult (int requestCode, int resultCode, Intent data)

在另一个Activity中获取数据的代码与上一个例子是相同的,实现如下:

/* 取得Intent中的Bundle对象 */ intent=this.getIntent(); bunde = intent.getExtras(); /* 取得Bundle对象中的数据 */ String sex = bunde.getString("sex");double height = bunde.getDouble("height"); 

下面给出这个例子的具体实现代码:

2.1  主程序的实现:

public class EX03_11 extends Activity {private EditText et; private RadioButton rb1; private RadioButton rb2; /** Called when the activity is first created. */ @Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 加载main.xml Layout */ setContentView(R.layout.main); /* 以findViewById()取得Button对象,并加入onClickListener */Button b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /*取得输入的身高*/ et = (EditText) findViewById(R.id.height);double height=Double.parseDouble(et.getText().toString()); /*取得选择的性别*/String sex="";rb1 = (RadioButton) findViewById(R.id.sex1); rb2 = (RadioButton) findViewById(R.id.sex2); if(rb1.isChecked()) { sex="M"; }else{sex="F"; } /*new一个Intent对象,并指定class*/ Intent intent = new Intent(); intent.setClass(EX03_11.this,EX03_11_1.class);/*new一个Bundle对象,并将要传递的数据传入*/Bundle bundle = new Bundle(); bundle.putDouble("height",height);bundle.putString("sex",sex); /*将Bundle对象assign给Intent*/ intent.putExtras(bundle); /*调用Activity EX03_11_1*/ startActivityForResult(intent,0); } }); } /* 重写 onActivityResult()*/@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { case RESULT_OK: /* 取得数据,并显示于画面上 */Bundle bunde = data.getExtras(); String sex = bunde.getString("sex");double height = bunde.getDouble("height"); et.setText(""+height);if(sex.equals("M")) {rb1.setChecked(true); }else{rb2.setChecked(true); } break;default: break;} } }

2.2 另一个Activity的实现:

public class EX03_11_1 extends Activity { Bundle bunde; Intent intent; /** Called when the activity is first created. */@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 载入mylayout.xml Layout */ setContentView(R.layout.myalyout);/* 取得Intent中的Bundle对象 */ intent=this.getIntent(); bunde = intent.getExtras(); /* 取得Bundle对象中的数据 */ String sex = bunde.getString("sex");double height = bunde.getDouble("height"); /* 判断性别 */String sexText="";if(sex.equals("M")) { sexText="男性"; } else{ sexText="女性"; } /* 取得标准体重 */String weight=this.getWeight(sex, height); /* 设定输出文字 */ TextView tv1=(TextView) findViewById(R.id.text1); tv1.setText("你是一位"+sexText+"\n你的身高是"+height+ "公分\n你的标准体重是"+weight+"公斤"); /* 以findViewById()取得Button对象,并加入onClickListener */ Button b1 = (Button) findViewById(R.id.button1);b1.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) { /* 回传result回上一个activity */ EX03_11_1.this.setResult(RESULT_OK, intent); /* 关闭activity */ EX03_11_1.this.finish(); } });} /* 四舍五入的method */ private String format(double num) {NumberFormat formatter = new DecimalFormat("0.00");String s=formatter.format(num); return s; }/* 以findViewById()取得Button对象,并加入onClickListener */private String getWeight(String sex,double height) { String weight=""; if(sex.equals("M")) {weight=format((height-80)*0.7);} else{weight=format((height-70)*0.6); } return weight; } }

不同Activity之间传递数据--Bundle对象和startActivityForResult方法的实现相关推荐

  1. android 不同activity之间传递数据

    1> 不同activity之间传递数据: Intent intent=new Intent(); intent.setClass(activity1.this,activity2.class); ...

  2. Android入门篇二:使用意图在Activity之间传递数据

    首先,在这里稍微介绍一下意图(Intent)的概念: Intent(意图)主要是解决Android应用的各项组件之间的通讯. Intent 负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述, ...

  3. AndroidStudio安卓原生开发_利用Activity的Intent 以及Bundle在activity之间传递数据---Android原生开发工作笔记91

    暂时不写内容,后边补上,因为工作太忙,先把图,以及重要的难点说明写出来,后边会修改成详细的文章

  4. Android TabHost中Activity之间传递数据

    例子1: TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);tabhost.setup(this.getLocalActiv ...

  5. Android Activity之间传递类对象

    一.简介 开发过程中,Activity之间传递数据是必不可少的,Android中使用Intent和Bundle作为数据载体,在Activity之间传递,对于基础数据类型,Bundle已经提供了相关的p ...

  6. 大叔也说Xamarin~Android篇~Activity之间传递数组

    大叔也说Xamarin~Android篇~Activity之间传递数组 原文:大叔也说Xamarin~Android篇~Activity之间传递数组 我们在开发应用程序时,不可能只使用一个Layout ...

  7. 父activity启动子activity并传递数据

    1. Intent component : activity,service,broadcast receiver以及content provider component 与操作系统 通信的一种媒介工 ...

  8. 安卓开发(5)-在 Activity 之间发送数据(一)

    前言 在Activity交互的过程中,肯定需要在两个Activity之间传递数据.根据您的 Activity 是否希望从即将启动的新 Activity 中获取返回结果,您可以使用 startActiv ...

  9. android activity之间传递对象,Android Activity之间的数据传递

    一.通过startActivity来进行Activity的传值 在Android中,如果我们要通过一个Activity来启动另一个Activity,可以使用 startActivity(Intent ...

最新文章

  1. 机电传动控制第二周学习笔记
  2. 第一章计算机基础知识第一节,第一章 计算机基础知识 第一节
  3. mapreduce编程实例python-使用Python语言写Hadoop MapReduce程序
  4. DB Query Analyzer 中断SQL语句的执行
  5. C#获取容器窗体中控件Location问题
  6. [Redux/Mobx] 说说Redux的实现流程
  7. Linux内核调试sysfs
  8. sis 最新_《炙热的我们》火箭少女首胜,sis姐妹花遭遇惨败出局?
  9. linux虚拟arm环境环境,Ubuntu 模拟ARM开发环境
  10. 操作文件和目录【TLCL】
  11. STM32F429HAL库时钟系统学习笔记
  12. 大厂程序员手把手教你如何写简历(附简历模板)
  13. 闲谈输入法、MinGW、日文字体
  14. 安航云酒店管理系统面试话术
  15. crc java代码_Java ZipEntry getCrc()用法及代码示例
  16. Golang 解析XML
  17. 我的阿里三面,四面分享给大家
  18. php下对港澳台身份证进行验证
  19. 市场分析文档(从0到1开始设计产品,明确你想做的产品的方向)
  20. matlab获取基金数据,读取WIND数据(行情、基金净值等)的SAS程序

热门文章

  1. Centos Ftp
  2. 机器学习十大算法(二)
  3. dhcp配置(个人)
  4. C# 制作开机自动启动程序
  5. Protobuf3 + Netty4: 在socket上传输多种类型的protobuf数据
  6. 小坑记录:get_cmap参数区分大小写
  7. 图片垂直居中的使用技巧
  8. linux中权限的修改
  9. USE SysBench test Mysql and PostgreSQL - 2
  10. Linux - 手册(manual)使用 详解