`
hejiaqi789
  • 浏览: 29424 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

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.classActivityandroid2.classActivity

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");

    android2onCreate方法中取值

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics