Android Intent 传值
Intent可以用来给Activity与Activity之间,Activity与Service之间传递参数。
1、传递基本数据类型。
2、String类型以及List.
3、有Serializablei和Parcelable接口的对象,对应的List也是可以传递的。
Intent intent = new Intent(this,SecondActivity.class);
//1、传递基本数据类型
intent.putExtra("data_int_key",11);
//2、String
ArrayList<String> strings = new ArrayList<>();
strings.add("ha ha");
strings.add("he he");
intent.putExtra("data_stringist_key",strings);
//3、object
public class IntentObject implements Serializable {
public String name;
public int age;
public IntentObject(String name,int age){
this.name = name;
this.age = age;
}
}
IntentObject intentObject = new IntentObject("xiao ming",18);
intent.putExtra("data_object_key",intentObject);
startActivity(intent);
接收数据:
int intData = getIntent().getIntExtra(“data_int_key”,0);
Log.e(“int value”,intData+”“);
ArrayList<String> strings =getIntent().getStringArrayListExtra("data_stringist_key");
Log.e("strings value",strings.toString());
IntentObject intentObject =(IntentObject)getIntent().getSerializableExtra("data_object_key");
Log.e("object value","name :"+intentObject.name+" age :"+intentObject.age);
本文介绍如何使用Android中的Intent来在Activity和服务间传递不同类型的参数,包括基本数据类型、String列表和实现了Serializable或Parcelable接口的对象。

3万+

被折叠的 条评论
为什么被折叠?



