1,新建一个activity,如Activity2,在清单文件AndroidManifest.xml 中 application节点中 增加一个新窗体:

.................

</activity>
<activity android:name=".ortherActivity" android:label="新窗口"/>
</application>

2,在主程序中:

Intent intent = new Intent(MainActivity.this, Activity2.class);

//打开新窗口。参数:主窗口,被调用窗口

Bundle bundle = new Bundle();//通过Bundle实现数据的传递:
      bundle.putString("key1", "value1");// key1为名,value1为值
      bundle.putString("key2", "value2");
      bundle.putInt("keyInt1", 100);
      intent.putExtras(bundle); // 传数据

如果不需要子窗口回传数据:
      startActivity(intent);

如果需要子窗口回传数据:

(1),startActivityForResult(intent, requestCode);

此处要注意,requestCode是子窗口的id标志,而且必须大于1,否则回调函数onActivityResult不响应!

(2),实现回调函数onActivityResult:

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  .....

}

主窗只有一个回调函数,如果调用了几个子窗口,就需通过requestCode来区分。而resultCode则是子窗口的返回值,可以用于后续的判断。

3,在新窗口中仍然使用Bundle取得数据:
  Bundle bundle = this.getIntent().getExtras();
  String s1 = bundle.getString("key1");
  String s2 = bundle.getString("key2");
  int i=bundle.getInt("keyInt1");

4,如何返回到主窗口?

Intent intent=new Intent();   
    Bundle bunle=new Bundle();   
    bunle.putString("key1", "这是子窗口的返回数据.");  
    intent.putExtras(bunle);
    //SubActivity.this.setResult(RESULT_OK, intent);
    //SubActivity.this.finish();
    setResult(RESULT_OK, intent);  //设置返回结果
    finish(); //关闭子窗口,否则数据无法返回

5,参考

http://blog.sina.com.cn/s/blog_63d409eb01017dnn.html

转自:http://blog.sina.com.cn/s/blog_3ecf39a70101axmo.html

android新窗口以及传值的更多相关文章

  1. android安卓开发基础小笔记,添加按钮事件,打开新窗体,窗体传值,回传

    给一个按钮添加onclick事件 //获取按钮对象 Button Aiyo = (Button)findViewById(R.id.button1); Aiyo.setOnClickListener( ...

  2. android 打开新窗口

    ImageView loginBtn = (ImageView)findViewById(R.id.login_button); loginBtn.setOnClickListener(new Vie ...

  3. Android 之窗口小部件详解(三)  部分转载

    原文地址:http://blog.csdn.net/iefreer/article/details/4626274. (一) 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget ...

  4. Android进阶之光-第1章-Android新特性-读书笔记

    第 1 章 Android 新特性 1.1 Android 5.0 新特性 1.1.1 Android 5.0 主要新特性 1. 全新的 Material Design 新风格 Material De ...

  5. Android 的窗口管理系统 (View, Canvas, WindowManager)

    http://blog.csdn.net/ritterliu/article/details/39295271 From漫天尘沙 在图解Android - Zygote 和 System Server ...

  6. JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题

    1.鼠标进入容器方向的判定 判断鼠标从哪个方向进入元素容器是一个经常碰到的问题,如何来判断呢?首先想到的是:获取鼠标的位置,然后经过一大堆的if..else逻辑来确定.这样的做法比较繁琐,下面介绍两种 ...

  7. html drag api 在firefox 下 拖动出现新窗口的解决办法

    有个功能,需要用drag drop api 来做. 发现在firefox下拖放,会出现新的tab 页签,即使在ondragover.ondrop中使用了event.preventDefault也无济于 ...

  8. window.open打开新窗口被浏览器拦截的处理方法

    一般我们在打开页面的时候, 最常用的就是用<a>标签,如果是新窗口打开就价格target="_blank"属性就可以了, 如果只是刷新当前页面就用window.loca ...

  9. angularjs 新窗口打开

    原文链接:angularjs 中state.go 跳转并且打开新的浏览器窗口 业务需要,需要点击打开一个新窗口,并且是点击事件触发的打开新窗口: $scope.lookLook =function(d ...

随机推荐

  1. Linux-ftp虚拟用户配置

    云服务器ESC 部署vsftpd 虚拟用户 说明:云服务器部署和本地服务器部署一样,都需要开通指定的相应端口,只不过云服务器需要在安全组规则中打开相应的端口允许通过. 环境说明: 对应的用户对应不同的 ...

  2. win10更改无线网卡的MAC地址

    https://blog.csdn.net/qq_31778495/article/details/80932472 前段时间电脑蹭网被禁了MAC地址,故寻找修改MAC地址的方法. 本机配置: win ...

  3. SQL随记(六)

    1.关于dbms_sql包的一些执行语句 cursor_name := DBMS_SQL.OPEN_CURSOR; --打开游标: DBMS_SQL.PARSE(cursor_name, var_dd ...

  4. CentOS7_防火墙

    1.查看防火墙状态 firewall-cmd --state 2.启动防火墙 systemctl start firewalld.service 3.关闭防火墙 systemctl stop fire ...

  5. Kubernetes fluentd+elasticsearch+kibana

    前提:dns服务,k8s集群 下载kubernetes,并解压 https://github.com/kubernetes/kubernetes/releases tar zxvf kubernete ...

  6. ZooKeeper-API 监听

    以服务动态上下线通知为例 Client 监听服务器状态 public class DistributeClient { private String connectString = "127 ...

  7. 为程序启用 守护进程-- supervisior

    待补充... Add this to your /etc/supervisord.conf: [rpcinterface:supervisor] supervisor.rpcinterface_fac ...

  8. Java使用AES算法进行加密解密

    一.加密 /** * 加密 * @param src 源数据字节数组 * @param key 密钥字节数组 * @return 加密后的字节数组 */ public static byte[] En ...

  9. 一次使用innobackupex重新搭建主从复制报错解决方法及注意事项

    [环境介绍] 系统环境:CentOS release 6.4 (Final) + Server version: 5.7.18-log MySQL Community Server (GPL) + i ...

  10. WebGL教程

    https://www.w3cschool.cn/webgl/rleo1oh7.html