Android工作学习第5天之Activity的传值问题
注:本文大部分为网上转载,本人只是根据工作的需要略做整合!
本章将借用一个实例,讲解如何注册并激活一个新的Activity,以及多个Activity之间如何传值。
下面是主Activity的代码:
[java] view plain copy print?
- package com.chaoyang.activity;
 - import android.app.Activity;
 - import android.content.Intent;
 - import android.os.Bundle;
 - import android.text.style.BulletSpan;
 - import android.view.View;
 - import android.widget.Button;
 - import android.widget.Toast;
 - publicclass MainActivity extends Activity {
 - /** Called when the activity is first created. */
 - @Override
 - publicvoid onCreate(Bundle savedInstanceState) {
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.main);
 - Button button =(Button)findViewById(R.id.button);
 - button.setOnClickListener(new View.OnClickListener() {
 - //给按钮注册点击事件,打开新的Acticity
 - @Override
 - publicvoid onClick(View v) {
 - // TODO Auto-generated method stub
 - //为Intent设置要激活的组件(将要激活TheOtherActivity这个Activity)
 - Intent intent =new Intent(MainActivity.this,TheOtherActivity.class);//
 - //写法一 intent.setClass(MainActivity.this, OtherActivity.class);//设置要激活的组件
 - //写法二 intent.setComponent(new ComponentName(MainActivity.this, TheOtherActivity.class));//设置要激活的组件
 - //第一种传值方式(代码看起来更加更简洁)
 - /*
 - intent.putExtra("name", "dinglang");
 - intent.putExtra("age", 22);
 - */
 - //第二种传值方式
 - Bundle bundle =new Bundle();
 - bundle.putString("name", "dinglang");
 - bundle.putInt("age", 22);
 - intent.putExtras(bundle);
 - /*
 - Intent提供了各种常用类型重载后的putExtra()方法,如: putExtra(String name, String value)、 putExtra(String name, long value),在putExtra()方法内部会判断当前Intent对象内部是否已经存在一个Bundle对象,如果不存在就会新建Bundle对象,以后调用putExtra()方法传入的值都会存放于该Bundle对象
 - 这些其实可以通过看源码的,内部实现的原理都是一样的
 - */
 - //startActivity(intent);//不需要接收组件的返回值,就可以直接这样激活了
 - //需要接收返回结果。注意返回的结果码
 - startActivityForResult(intent, 100);
 - }
 - });
 - }
 - @Override
 - protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
 - // TODO Auto-generated method stub
 - Toast.makeText(this, data.getStringExtra("result"), 1).show();//得到返回结果
 - super.onActivityResult(requestCode, resultCode, data);
 - }
 - }
 
下面是otherActivity部分代码:
在相同包下,新建一个类,继承至Activity这个类,重写onCreate方法...
- package com.chaoyang.activity;
 - import android.app.Activity;
 - import android.content.Intent;
 - import android.os.Bundle;
 - import android.view.View;
 - import android.widget.Button;
 - import android.widget.TextView;
 - publicclass TheOtherActivity extends Activity {
 - @Override
 - protectedvoid onCreate(Bundle savedInstanceState) {
 - // TODO Auto-generated method stub
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.other);//设置该Activity所对应的xml布局文件
 - Intent intent =this.getIntent();//得到激活她的意图
 - String name =intent.getStringExtra("name");
 - int age=intent.getExtras().getInt("age");//第二种取值方式
 - TextView textView = (TextView)this.findViewById(R.id.result);
 - textView.setText("姓名:"+ name+" 年龄:"+ age);
 - Button button = (Button)this.findViewById(R.id.close);
 - button.setOnClickListener(new View.OnClickListener() {
 - //返回结果给前面的Activity
 - @Override
 - publicvoid onClick(View v) {
 - // TODO Auto-generated method stub
 - Intent intent =new Intent();
 - intent.putExtra("result", "这是处理结果");
 - setResult(20, intent);//设置返回数据
 - finish();//关闭activity
 - }
 - });
 - }
 - }
 
新建Activity之间,注意要在layout文件夹中新建一个XML的布局文件。(新建Android项目时如果选择了创建Activity,会默认新建一个XML的布局文件)
下面是布局文件main.xml:
- <?xmlversion="1.0"encoding="utf-8"?>
 - <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
 - android:orientation="vertical"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - >
 - <TextView
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:text="@string/hello"
 - />
 - <Button
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="打开OtherActivity"
 - android:id="@+id/button"
 - />
 - </LinearLayout>
 
下面是布局文件other.xml
[html] view plain copy print?
- <?xmlversion="1.0"encoding="utf-8"?>
 - <LinearLayout
 - xmlns:android="http://schemas.android.com/apk/res/android"
 - android:orientation="vertical"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent">
 - <TextView
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:text="这是OtherActivity"
 - android:id="@+id/result"
 - />
 - <Button
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="关闭Activity"
 - android:id="@+id/close"
 - />
 - </LinearLayout>
 
最后,注意修改项目清单文件。在里面添加,注册新的Acticity名称
[html] view plain copy print?
- <?xmlversion="1.0"encoding="utf-8"?>
 - <manifestxmlns:android="http://schemas.android.com/apk/res/android"
 - package="com.chaoyang.activity"
 - android:versionCode="1"
 - android:versionName="1.0">
 - <uses-sdkandroid:minSdkVersion="8"/>
 - <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
 - <activityandroid:name=".MainActivity"
 - android:label="@string/app_name">
 - <intent-filter>
 - <actionandroid:name="android.intent.action.MAIN"/>
 - <categoryandroid:name="android.intent.category.LAUNCHER"/>
 - </intent-filter>
 - </activity>
 - <!-- 注意项目清单文件中要加上 -->
 - <activityandroid:name="TheOtherActivity"android:label="the other Activity"/>
 - </application>
 - </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的传值问题的更多相关文章
- Android工作学习第5天之Activity的完全退出程序
		
注:本文大部分为网上转载,本人只是根据工作的需要略做整合! android 完全退出应用程序 注意:1.单例模式的学习 2.Manifest.xml,注意项目清单文件中要加上 android退出应用程 ...
 - Android工作学习第5天之TabHost实现菜单栏底部显示
		
TabHost是一个装载选项卡窗口的容器,实现分模块显示的效果.像新浪微博客户端.微信客户端都是使用tabehost组件来开发的. TabHost的组成: |---TabWidget:实现标签栏,可供 ...
 - Android开发学习之路-Service和Activity的通信
		
在很多时候,Service都不仅仅需要在后台运行,还需要和Activity进行通信,或者接受Activity的指挥,如何来实现,来看代码. 定义一个服务 // 创建一个服务,然后在onBind()中返 ...
 - 【Android开发学习笔记】【第五课】Activity的生命周期-上
		
今天学习Activity当中的七个生命周期函数: 首先得说一个事情,就是在代码当中如果加入了 System.out.println(" ------");之后,如何查看这里面的输出 ...
 - 【Android开发学习笔记】【第三课】Activity和Intent
		
首先来看一个Activity当中启动另一个Activity,直接上代码说吧: (1)首先要多个Activity,那么首先在res-layout下新建一个 Other.xml,用来充当第二个Activi ...
 - Android开发学习之路--Activity之初体验
		
环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...
 - Android学习笔记(一)——Activity简介 和 View
		
源文链接:http://www.cnblogs.com/shyang--TechBlogs/archive/2011/03/14/1984195.html Android SDK ( Software ...
 - Android学习总结(四)—— Activity和 Service进行通信
		
一.Activity 和 Service进行通信的基本概念 前面我们学习我生命周期里面包含了启动和停止服务的方法,虽然服务器在活动里启动,但在启动了服务之后,活动与服务基本就没有什么关系了.我们在活动 ...
 - Android开发学习—— activity
		
activity生命周期 #Activity生命周期###void onCreate()* Activity已经被创建完毕###void onStart()* Activity已经显示在屏幕,但没有得 ...
 
随机推荐
- js的一点
			
1.js中实现继承的方法 1.js原型(prototype)实现继承 <SPAN style="<SPAN style="FONT-SIZE: 18px"&g ...
 - Bash条件判断
			
bash编程之:条件判断,判定后续操作的前提条件是否满足, bash编程之: 条件判断常用类型: 整数测试:比较两个整数谁大谁小,是否相等: 二元测试: num1 操作符 num2 -eq: 等于 - ...
 - rabbitmq 重复ACK导致消息丢失
			
rabbitmq 重复确认导致消息丢失 背景 rabbitmq 在应用场景中,大多采用工作队列 work-queue的模式. 在一个常见的工作队列模式中,消费者 worker 将不断的轮询从队列中拉取 ...
 - CentOS 6、7下pptp vpn一键安装脚本
			
之前有折腾过<CentOS 6.7下IPSEC/L2TP VPN一键安装脚本>,不稳定.不支持IOS,因此换成pptp,并已经添加到<lnmp一键安装包>.这个脚本可以单独使用 ...
 - sql server中批量插入与更新两种解决方案分享
			
若只是需要大批量插入数据使用bcp是最好的,若同时需要插入.删除.更新建议使用SqlDataAdapter我测试过有很高的效率,一般情况下这两种就满足需求了 bcp方式 复制代码 代码如下: /// ...
 - [Spring MVC] - 从数据库读取MessageSource
			
Spring MVC中使用MessageSource默认是写在properties文件当中,以支持国际化. 但很多时候我们需要把数据写到数据库当中,而不是在properties文件当中,以方便日常维护 ...
 - C#:比较二个字符串,查找出相同字数和差异字符
			
;; i < n; i++) { ) == s2.Substring(i, )) /*同位 ...
 - 如何在 Delphi 中静态链接 SQLite
			
搞了我几个小时,终于成功在 Delphi 中静态链接了 SQLite (v3.5.4),下一步就是研究加密了,呵呵中间其实遇到很多问题,今天累了,就不说了,改天补上 下载测试工程 下面说说方法 1.当 ...
 - CFDebug.template
			
{ "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "Cre ...
 - 测试 ClownFish、CYQ、Entity Framework、Moon、MySoft、NHibernate、PDF、XCode数据访问组件性能
			
下期预告: 由于很多园友反馈,有的组件不应该缺席.测试复杂度不够.测试还缺乏一定的公平. 因此考虑在下一个版本中,确保在更加公平的前提下进行更高复杂度的测试 . 同时将分为2组测试,纯SQL组件及纯O ...