Intent学习

Intent有六个属性

Component Name(组件名),Action(动作),Data(数据),Categry(分类),Extra(额外信息),Flag(标志)

传递数据的功能通过Extra实现的,Intent在传递数据的时候,发起方要先封装数据,接收方根据数据I封装格式接受并处理数据

显示Intent,常见的有四种

                               第一种
				Intent intent =new Intent(Intent_learn.this,second_intent.class);
		               startActivity(intent);
		               第二种
				Intent intent = new Intent();
				ComponentName cn =new ComponentName(Intent_learn.this,second_intent.class);
				intent.setComponent(cn);
				startActivity(intent);
				第三种
				Intent intent = new Intent();
				intent.setClass(Intent_learn.this, second_intent.class);
				startActivity(intent);
				第四种
				Intent intent = new Intent();
				intent.setClassName(getBaseContext(), second_intent.class.getName());
				startActivity(intent);

通过Action和data相结合,Intent可以实现访问网页,打电话,发短信等功能


发送方式常见的有两种

1、键值对形式

发送方的按钮响应:

			public void onClick(View v) {
				Intent intent =new Intent(Intent_learn.this,second_intent.class);
				intent.putExtra("user", et1.getText().toString());
				intent.putExtra("psw", et2.getText().toString());
				startActivity(intent);
				
			}

接收方:

	   TextView tv1 =(TextView) findViewById(R.id.second_id1);
		TextView tv2 =(TextView) findViewById(R.id.second_id2);
		Intent intent =getIntent();
		user=intent.getStringExtra("user");
		psw =intent.getStringExtra("psw");
	      tv1.setText("user:     "+user);
		tv2.setText("password: "+psw);



2、使用Bundle数据包

发送方的按钮响应:

			  public void onClick(View v) {
				Intent intent =new Intent(Intent_learn.this,second_intent.class);
				Bundle bd = new Bundle();
				bd.putString("user",  et1.getText().toString());
				bd.putString("psw",  et2.getText().toString());
				System.out.println("user:"+et1.getText().toString());
				System.out.println("user:"+et2.getText().toString());
				intent.putExtras(bd);
				startActivity(intent);
				
			}

接收方:

	      TextView tv1 =(TextView) findViewById(R.id.second_id1);
		TextView tv2 =(TextView) findViewById(R.id.second_id2);
		Intent intent =getIntent();
		Bundle  bundle =intent.getExtras();
		user=bundle.getString("user");
		psw=bundle.getString("psw");
	       tv1.setText("user:     "+user);
		tv2.setText("password: "+psw);

它支持发送多种数据发送,只是举例的时候,只举了string


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值