注:本文大部分为网上转载,本人只是根据工作的需要略做整合!

本章将借用一个实例,讲解如何注册并激活一个新的Activity,以及多个Activity之间如何传值。

下面是主Activity的代码:

[java] view plain copy print?

  1. package com.chaoyang.activity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.text.style.BulletSpan;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.Toast;
  9. publicclass MainActivity extends Activity {
  10. /** Called when the activity is first created. */
  11. @Override
  12. publicvoid onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. Button button =(Button)findViewById(R.id.button);
  16. button.setOnClickListener(new View.OnClickListener() {
  17. //给按钮注册点击事件,打开新的Acticity
  18. @Override
  19. publicvoid onClick(View v) {
  20. // TODO Auto-generated method stub
  21. //为Intent设置要激活的组件(将要激活TheOtherActivity这个Activity)
  22. Intent intent =new Intent(MainActivity.this,TheOtherActivity.class);//
  23. //写法一 intent.setClass(MainActivity.this, OtherActivity.class);//设置要激活的组件
  24. //写法二 intent.setComponent(new ComponentName(MainActivity.this, TheOtherActivity.class));//设置要激活的组件
  25. //第一种传值方式(代码看起来更加更简洁)
  26. /*
  27. intent.putExtra("name", "dinglang");
  28. intent.putExtra("age", 22);
  29. */
  30. //第二种传值方式
  31. Bundle bundle =new Bundle();
  32. bundle.putString("name", "dinglang");
  33. bundle.putInt("age", 22);
  34. intent.putExtras(bundle);
  35. /*
  36. Intent提供了各种常用类型重载后的putExtra()方法,如: putExtra(String name, String value)、 putExtra(String name, long value),在putExtra()方法内部会判断当前Intent对象内部是否已经存在一个Bundle对象,如果不存在就会新建Bundle对象,以后调用putExtra()方法传入的值都会存放于该Bundle对象
  37. 这些其实可以通过看源码的,内部实现的原理都是一样的
  38. */
  39. //startActivity(intent);//不需要接收组件的返回值,就可以直接这样激活了
  40. //需要接收返回结果。注意返回的结果码
  41. startActivityForResult(intent, 100);
  42. }
  43. });
  44. }
  45. @Override
  46. protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
  47. // TODO Auto-generated method stub
  48. Toast.makeText(this, data.getStringExtra("result"), 1).show();//得到返回结果
  49. super.onActivityResult(requestCode, resultCode, data);
  50. }
  51. }

下面是otherActivity部分代码:

在相同包下,新建一个类,继承至Activity这个类,重写onCreate方法...

  1. package com.chaoyang.activity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. publicclass TheOtherActivity extends Activity {
  9. @Override
  10. protectedvoid onCreate(Bundle savedInstanceState) {
  11. // TODO Auto-generated method stub
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.other);//设置该Activity所对应的xml布局文件
  14. Intent intent =this.getIntent();//得到激活她的意图
  15. String name =intent.getStringExtra("name");
  16. int age=intent.getExtras().getInt("age");//第二种取值方式
  17. TextView textView = (TextView)this.findViewById(R.id.result);
  18. textView.setText("姓名:"+ name+"  年龄:"+ age);
  19. Button button = (Button)this.findViewById(R.id.close);
  20. button.setOnClickListener(new View.OnClickListener() {
  21. //返回结果给前面的Activity
  22. @Override
  23. publicvoid onClick(View v) {
  24. // TODO Auto-generated method stub
  25. Intent intent =new Intent();
  26. intent.putExtra("result", "这是处理结果");
  27. setResult(20, intent);//设置返回数据
  28. finish();//关闭activity
  29. }
  30. });
  31. }
  32. }

新建Activity之间,注意要在layout文件夹中新建一个XML的布局文件。(新建Android项目时如果选择了创建Activity,会默认新建一个XML的布局文件)

下面是布局文件main.xml:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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="@string/hello"
  11. />
  12. <Button
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="打开OtherActivity"
  16. android:id="@+id/button"
  17. />
  18. </LinearLayout>

下面是布局文件other.xml

[html] view plain copy print?

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="这是OtherActivity"
  11. android:id="@+id/result"
  12. />
  13. <Button
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="关闭Activity"
  17. android:id="@+id/close"
  18. />
  19. </LinearLayout>

最后,注意修改项目清单文件。在里面添加,注册新的Acticity名称

[html] view plain copy print?

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.chaoyang.activity"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdkandroid:minSdkVersion="8"/>
  7. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  8. <activityandroid:name=".MainActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <actionandroid:name="android.intent.action.MAIN"/>
  12. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  13. </intent-filter>
  14. </activity>
  15. <!-- 注意项目清单文件中要加上 -->
  16. <activityandroid:name="TheOtherActivity"android:label="the other Activity"/>
  17. </application>
  18. </manifest>
 

需要注意的知识点:

使用Intent组件附件数据时候,为Activity之间传值的两种写法。

值得一提的是Bundle类的作用

Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值。相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,如:putString()/getString()和putInt()/getInt(),putXxx()用于往Bundle对象放入数据,getXxx()方法用于从Bundle对象里获取数据。Bundle的内部实际上是使用了HashMap<String, Object>类型的变量来存放putXxx()方法放入的值。

还有就是在onActivityResult这个方法中,第一个参数为请求码,即调用startActivityForResult()传递过去的值 ,第二个参数为结果码,结果码用于标识返回数据来自哪个新Activity。都是起简单的标识作用的(不要和http协议中的404,200等状态码搞混了),可以根据自己的业务需求填写,匹配,必要时候可以根据这个去判断。

转载地址:http://blog.csdn.net/dinglang_2009/article/details/6888111

Android工作学习第5天之Activity的传值问题的更多相关文章

  1. Android工作学习第5天之Activity的完全退出程序

    注:本文大部分为网上转载,本人只是根据工作的需要略做整合! android 完全退出应用程序 注意:1.单例模式的学习 2.Manifest.xml,注意项目清单文件中要加上 android退出应用程 ...

  2. Android工作学习第5天之TabHost实现菜单栏底部显示

    TabHost是一个装载选项卡窗口的容器,实现分模块显示的效果.像新浪微博客户端.微信客户端都是使用tabehost组件来开发的. TabHost的组成: |---TabWidget:实现标签栏,可供 ...

  3. Android开发学习之路-Service和Activity的通信

    在很多时候,Service都不仅仅需要在后台运行,还需要和Activity进行通信,或者接受Activity的指挥,如何来实现,来看代码. 定义一个服务 // 创建一个服务,然后在onBind()中返 ...

  4. 【Android开发学习笔记】【第五课】Activity的生命周期-上

    今天学习Activity当中的七个生命周期函数: 首先得说一个事情,就是在代码当中如果加入了 System.out.println(" ------");之后,如何查看这里面的输出 ...

  5. 【Android开发学习笔记】【第三课】Activity和Intent

    首先来看一个Activity当中启动另一个Activity,直接上代码说吧: (1)首先要多个Activity,那么首先在res-layout下新建一个 Other.xml,用来充当第二个Activi ...

  6. Android开发学习之路--Activity之初体验

    环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...

  7. Android学习笔记(一)——Activity简介 和 View

    源文链接:http://www.cnblogs.com/shyang--TechBlogs/archive/2011/03/14/1984195.html Android SDK ( Software ...

  8. Android学习总结(四)—— Activity和 Service进行通信

    一.Activity 和 Service进行通信的基本概念 前面我们学习我生命周期里面包含了启动和停止服务的方法,虽然服务器在活动里启动,但在启动了服务之后,活动与服务基本就没有什么关系了.我们在活动 ...

  9. Android开发学习—— activity

    activity生命周期 #Activity生命周期###void onCreate()* Activity已经被创建完毕###void onStart()* Activity已经显示在屏幕,但没有得 ...

随机推荐

  1. jstl 标签库的使用

    JSTL 核心标签库 使用   JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.ot ...

  2. String、StringBuffer和StringBuilder的深入解析

    今天闲来无事,整理了下平时记录在印象笔记里的java开发知识点,整理到String,StringBuffer以及StringBuilder的区别时突然又产生了新的疑惑,这些区别是怎么产生的?温故为何能 ...

  3. 每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。10元最多可喝多少瓶啤酒? php

    //根据题意设定初始变量值//然后 一个while循环去操作//每换得一个瓶子,换的那个减去要做相应的数目//然后喝了啤酒后, 各数量加1//直到不符合要求.跳出循环 1 class Beer{ pr ...

  4. Java操作redis简单示例

    第一:安装Redis    首先我们要安装Redis,就像我们操作数据库一样,在操作之前肯定要先创建好数据库的环境.    Redis的下载可以百度一下,或者打开下面的下载链接:    https:/ ...

  5. css伪类选择器及伪元素选择器

    1.类选择器 在css中可以使用类选择器把相同的元素定义成不同的样式.比如: 结果如下: 标题背景未变 2.伪类选择器 类选择器和伪类选择器的区别在于,类选择器我们定义的,而伪类选择器是CSS中已经定 ...

  6. PHP文件和目录操作-----复制、移动、重命名、删除文件

    PHP通过copy()函数来复制一个文件.用法如下: bool copy(string $source, string $dest) 其中$source是源文件的路径,$dest是目的文件的路径.函数 ...

  7. [BZOJ 3637]Query on a tree VI

    偶然看见了这题,觉得自己 QTREE.COT 什么的都没有刷过的真是弱爆了…… 一道思路很巧妙的题,终于是在约大爷的耐心教导下会了,真是太感谢约大爷了. 这题显然是树链剖分,但是链上维护的东西很恶心. ...

  8. 利用web工具splinter模拟登陆做自动签到

    首先,我需要的工具和组件有: Chrome浏览器 浏览器驱动ChromeDriver Python 3.5 Web应用测试工具Splinter 代码部分: from splinter import B ...

  9. 查看哪些表的哪些列含有指定字符串(如‘andy’存在哪些表的哪些列中)

    -- 查看表中列含有指定字符.SQL> select * from demo1;NAME                                   ID---------------- ...

  10. java获得某个月的天数

    /*** 方法一*/ String strDate = "2012-02"; SimpleDateFormat format = new SimpleDateFormat(&quo ...