Android最简单数据传递之利用Intent对象携带简单数据

 利用Intent对象携带简单数据

 利用Intent的Extra部分来存储我们想要传递的数据,可以传送String , int, long, char等一些基础类型,对复杂的对象就无能为力了。

1,设置参数

 //传递些简单的参数
             Intent intent1 = new Intent();
             intent1.setClass(MainActivity.this,SimpleActivity.class);

             //intent1.putExtra("usr", "lyx");
             //intent1.putExtra("pwd", "123456");
             //startActivity(intent1);

             Bundle bundleSimple = new Bundle();
             bundleSimple.putString("usr", "lyx");
             bundleSimple.putString("pwd", "123456");
             intent1.putExtras(bundleSimple);

             startActivity(intent1);

2,接收参数

//接收参数

           //Intent intent = getIntent();
           //String eml = intent.getStringExtra("usr");
           //String pwd = intent.getStringExtra("pwd");

           Bundle bundle = this.getIntent().getExtras();
           String eml = bundle.getString("usr");
           String pwd = bundle.getString("pwd");

在这种情况下 , 有些童鞋喜欢直接用intent传递数据 , 不经过bundle来传递数据 . 其实这两种方式没有区别的 , 查看Intent 的源码就会发现 , 其实intent1.putExtra也是通过bundle来传递。

猜你喜欢

转载自blog.csdn.net/weixin_42744183/article/details/89184923