Android Intent 传递简单数据

Android Intent 传递简单数据可以直接调用 putExtra() 和 getXxxExtra()

传递一个参数

如果只是传递一个参数,可以直接调用 putExtra() 和 getExtra()

写数据

Intent it = new Intent(A.this,B.class);
it.putExtra("key",value);
startActivity(it);

读数据

Intent it = getIntent();

//不同的数据类型,只要替换 String 即可
it.getStringExtra("key");

一次性传递多个参数

传递多个的话,可以使用 Bundle 对象作为容器

  1. 调用 Bundle 的 putXxx() 先将数据存储到 Bundle中
  2. 调用 Intent 的 putExtras() 方法将 Bundle 存入 Intent 中
  3. 读数据的时候,获得 Intent 以后,调用 getExtras() 获得 Bundle 容器
  4. 调用其 getXXX() 获取对应的数据

写数据

Intent it = new Intent(A.this,B.class);
Bundle bd = new Bundle();
bd.putInt("num",1);
bd.putString("name","yufei");

it.putExtra(bd);
startActivity(it);

读数据

Intent it = getIntent();

Bundle bd = it.getExtra();
int num = bd.getInt("num");
String name = bd.getString("name");

Android 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

♥   简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.