欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 多个Activity之间的传值

程序员文章站 2023-11-22 08:01:03
下面是主activity的代码: 开发:activity之间的传值 - 51cto.com复制代码 代码如下:package com.chaoyang.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等状态码搞混了),可以根据自己的业务需求填写,匹配,必要时候可以根据这个去判断。

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