在Activity之间传递参数(二)

传递数据包bundle:

1、MainActivity.class:

    findViewById(R.id.btnStartAty).setOnClickListener(new View.OnClickListener(){
      @Override
      public void onClick(View v) {
        Intent i = new Intent(MainActivity.this,TheAty.class);

        Bundle b = new Bundle();
        b.putString("name","Android");
        b.putInt("age",2);
        //b.putString("name1","Android");

        //i.putExtras(b);
        i.putExtra("data",b);

        startActivity(i);
      }
    });

2、TheAty.class:

    Intent i = getIntent();

    //Bundle data = i.getExtras();
    Bundle data = i.getBundleExtra("data");

    tv =(TextView)findViewById(R.id.tv);
    //name1不存在时默认w,存在时即为该值
    tv.setText(String.format("name=%s,age=%d,name1=%s",data.getString("name"),data.getInt("age"),data.getString("name1","w")));

上一篇:个人作业2 英语学习APP分析


下一篇:必应词典英语学习APP案例分析