android之intent学习

Intent传值

       EditText et = (EditText)findViewById(R.id.etName);//定义的文本框。

    String aa = et.getText().toString();//获取文本框内容。

       Intent intent = new Intent();

intent.setClass(helloworld.this,android2.class);//helloworld.class为Activity,android2.class为Activity。

intent.putExtra("name", "传值测试!");//设置传递内容。

    startActivity(intent);//跳转。

    finish();//关闭当前Activity。

第一种写法,用于批量添加数据到Intent:

   Intent intent = new Intent();

  Bundle bundle = new Bundle();//该类用作携带数据

  bundle.putString("name", "学习android");

   intent.putExtras(bundle);//为意图追加额外的数据,意图原来已经具有的数据不会丢失,但key同名的数据会被替换

第二种写法:这种写法的作用等价于上面的写法,只不过这种写法是把数据一个个地添加进Intent,这种写法使用起来比较方便,而且只需要编写少量的代码。

   Intent intent = new Intent();

  intent.putExtra("name", "学习android");

    在android2的onCreate方法中取值

【取值方法:String test= (String)(this.getIntent().getExtras().getString("name").toString());】

原文地址:https://www.cnblogs.com/hyzhou/p/3025837.html