多个Activity之间传值

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

下面是主Activity的代码:

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

下面是otherActivity部分代码:

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

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

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

下面是布局文件main.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.       
  13.     <Button    
  14.         android:layout_width="wrap_content"   
  15.         android:layout_height="wrap_content"   
  16.         android:text="打开OtherActivity"  
  17.         android:id="@+id/button"  
  18.         />  
  19. </LinearLayout>  

下面是布局文件other.xml

[html]  view plain copy
  1. <?xml version="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.     
  8.   <TextView    
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="这是OtherActivity"  
  12.     android:id="@+id/result"  
  13.     />  
  14.       
  15.       <Button    
  16.     android:layout_width="wrap_content"   
  17.     android:layout_height="wrap_content"   
  18.     android:text="关闭Activity"  
  19.     android:id="@+id/close"  
  20.     />  
  21. </LinearLayout>  

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

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.chaoyang.activity"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".MainActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.         <!-- 注意项目清单文件中要加上 -->  
  17. <activity android:name="TheOtherActivity" android:label="the other Activity"/>  
  18.     </application>  
  19. </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等状态码搞混了),可以根据自己的业务需求填写,匹配,必要时候可以根据这个去判断。

这里就不做深入的讲解了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值