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