Android开发基础之Activity之间参数传递
一、简单数值传递
1、MainActivity
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"))); Intent i=new Intent(MainActivity.this,NewActivity.class); i.putExtra("data","我是要传递的字符串"); startActivity(i); } }); }2、NewActivity
private TextView T; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new); Intent i=getIntent(); T=(TextView) findViewById(R.id.textView1); T.setText(i.getStringExtra("data")); }
二、数据包Bundle传递
1、MainActivity
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i=new Intent(MainActivity.this,NewActivity.class); Bundle B=new Bundle(); B.putString("name","张三"); B.putInt("age",23); i.putExtra("data",B); startActivity(i); } }); }2、NewActivity
private TextView T; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new); Intent i=getIntent(); Bundle b=new Bundle(); b=i.getBundleExtra("data"); T=(TextView) findViewById(R.id.textView1); T.setText(String.format("name:%s",b.getString("name"))); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。