zoukankan      html  css  js  c++  java
  • Android Activity 传递数据

    activity中数据的传递方式有2中,一种是使用putExtra()传递,另外一种是传递Bundle对象,使用putExtras()方法。

    方法一

    • 发送数据

    putExtra()传送的是键值对,第一个参数是key,第二个参数是value。

    	Intent intent = new Intent(MainActivity.this, Activity2.class);
    	intent.putExtra("name", "hello");
    	startActivity(intent);
    
    • 接收数据

    getString(): Extra根据key,获取value的值。

    	Intent intent = getIntent();
    	String name = intent.getStringExtra("name");
    

    方法二

    • 发送数据

    使用bundle传输数据,putString(),putInt(),第一个参数key,第二个参数value.

    可以传递String,int类型,或者其他。

    	Intent i = new Intent(MainActivity.this, Activity3.class);
    	Bundle bundle = new Bundle();
    	bundle.putString("name", "hello");
    	bundle.putInt("grade", 10);
    	i.putExtras(bundle);
    	startActivity(i);
    
    • 接收数据
    	Intent intent = getIntent();
    	Bundle bundle = intent.getExtras();
    	String name = bundle.getString("name");
    	int grade = bundle.getInt("grade");
    

    Example

    源码地址:https://github.com/TonySudo/ActivityData

    设置2个按键分别用于测试2种方法传送的数据。在MainActivity之外再创建2个activity。

    不同的按键启动不同的Activity,传递的方式也不同。

    显示效果:

    点击第一个按键

    点击第二个按键

    显示效果是一样的,只是内部传递的数据方式不同。

    Tony Liu

    2017-3-10, Shenzhen

  • 相关阅读:
    splay复杂度的证明
    splay的写法
    洛谷 P3722 [AH2017/HNOI2017]影魔
    洛谷 P4770 [NOI2018]你的名字
    清北考前刷题day3下午好
    P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)
    bzoj3252攻略(线段树+dfs序)
    清北考前刷题day2早安
    清北考前刷题day2下午好
    清北考前刷题day1下午好
  • 原文地址:https://www.cnblogs.com/helloworldtoyou/p/6530163.html
Copyright © 2011-2022 走看看