Android 多个Activity之间的传值

 更新时间:2013年11月01日 09:06:25   作者:  
本篇文章将用一个实例,详细的为大家讲解怎么注册并激活一个新的Activity,以及多个Activity之间如何传值

下面是主Activity的代码:

开发:Activity之间的传值 - 51CTO.COM

复制代码 代码如下:

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;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button =(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

   //给按钮注册点击事件,打开新的Acticity
         @Override
   public void 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
 protected void 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;

public class TheOtherActivity extends Activity {

 @Override
 protected void 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
   public void 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:

复制代码 代码如下:

<?xml version="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="@string/hello"
    />

    <Button 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="打开OtherActivity"
     android:id="@+id/button"
     />
</LinearLayout>

下面是布局文件other.xml
复制代码 代码如下:

<?xml version="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名称
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.chaoyang.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  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 android: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等状态码搞混了),可以根据自己的业务需求填写,匹配,必要时候可以根据这个去判断。

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

相关文章

  • 详解Flutter扫码识别二维码内容

    详解Flutter扫码识别二维码内容

    这篇文章主要介绍了Flutter扫码识别二维码内容的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Android Okhttp断点续传面试深入解析

    Android Okhttp断点续传面试深入解析

    这篇文章主要给大家介绍了关于Android Okhttp断点续传面试的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Android具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • 下载、编译、运行android 7.1系统详解(ubuntu 16.0.4)

    下载、编译、运行android 7.1系统详解(ubuntu 16.0.4)

    Android 7的系统版本新增的很多的新功能,本篇文章主要介绍了基于ubuntu 16.0.4环境的下载、编译、运行android 7.1系统,有兴趣的可以了解一下。
    2017-01-01
  • Android自定义播放器控件VideoView

    Android自定义播放器控件VideoView

    这篇文章主要介绍了Android自定义播放器控件VideoView的相关资料,需要的朋友可以参考下
    2016-01-01
  • Android数据缓存框架内置ORM功能使用教程

    Android数据缓存框架内置ORM功能使用教程

    这篇文章主要为大家介绍了Android数据缓存框架内置ORM功能使用教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Android实现布局全屏

    Android实现布局全屏

    这篇文章主要为大家详细介绍了Android实现布局全屏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • Android通过实现GridView的横向滚动实现仿京东秒杀效果

    Android通过实现GridView的横向滚动实现仿京东秒杀效果

    这篇文章主要介绍了Android通过实现GridView的横向滚动实现仿京东秒杀效果,实现代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07
  • Android SeekBar控制视频播放进度实现过程讲解

    Android SeekBar控制视频播放进度实现过程讲解

    这篇文章主要介绍了Android SeekBar控制视频播放进度实现过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-04-04
  • Android实现Service重启的方法

    Android实现Service重启的方法

    这篇文章主要介绍了Android实现Service重启的方法,涉及Android操作Service组件实现服务重启的功能,需要的朋友可以参考下
    2015-05-05
  • 详解Android使用Gradle统一配置依赖管理

    详解Android使用Gradle统一配置依赖管理

    本篇文章主要介绍了详解Android 使用 Gradle 统一配置依赖管理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01

最新评论