Intent和Bundle使用方法

1. MainActivity

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button botton3 = (Button)findViewById(R.id.button1); 
		botton3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this, ShowActivity.class);  
				intent.putExtra("name", "雨松MOMO");
				intent.putExtra("age", 25); 
				intent.putExtra("boy", true);  
				
				Bundle bundle = new Bundle(); 
				bundle.putString("b_name", "小可爱"); 
				bundle.putInt("b_age", 23);
				bundle.putBoolean("b_boy", false); 
				intent.putExtras(bundle); 
				startActivity(intent); 
			} 
		});
	}
}

2. ShowActivity

public class ShowActivity extends Activity { 
	protected void onCreate(Bundle savedInstanceState) { 
		setContentView(R.layout.my); 
		Intent intent = getIntent(); 
		String name = intent.getStringExtra("name"); 
		int age  = intent.getIntExtra("age", 0); 
		boolean isboy = intent.getBooleanExtra("boy", false);
		TextView textView0 = (TextView)findViewById(R.id.textView1);
		textView0.setText("姓名  " + name + "年龄 " + age + "男孩?  " + isboy);  
		
		Bundle bundle = intent.getExtras(); 
		name = bundle.getString("b_name"); 
		age = bundle.getInt("b_age",0);
		isboy = bundle.getBoolean("b_boy", false); 
		TextView textView1 = (TextView)findViewById(R.id.textView2);
		textView1.setText("姓名  " + name + "年龄 " + age + "男孩?  " + isboy); 
		
		super.onCreate(savedInstanceState);
	}
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值