Activity之间传递数据的三种方式详解

Activity之间传递数据有三种方式

1.通过Bundle传递简单数据
2.通过Serializable方式传递对象
3.通过Parcelable方式传递对象

通过Bundle对象传递数据的代码:

//点击按钮传递数据的方法
	public void button1(View v) {
		
		
		Intent intent=new Intent(this,Activity2.class);
		
		String s=editText1.getText().toString();
		
		//通过Bundle对象传递数据
		Bundle bundle=new Bundle();
		bundle.putString("info",s);
		intent.putExtra("box",bundle);
		startActivity(intent);

	}

通过Bundle对象接收数据的代码:

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity2);
		
		textView1=(TextView) findViewById(R.id.textView1);
		
		//获取Intent
		Intent intent =getIntent();
	    //通过Intent获取bundle对象
		Bundle bundle=intent.getBundleExtra("box");
		//取出数据
	    String s=bundle.getString("info");
	    textView1.setText(s);
	    //最后给textView设置进去并显示出来
	}

Bundle传递数据小结

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章