android开发intent传值,Android——Intent传值1、使⽤putExtra()和getIntent()⽅式传值。⼀般只能传基本类型数据。
private static final String EXTRA_NAME= "name";//键
private String name = "Tom";
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
//1.将所传值以键值对的形式存放到intent中。
intent.putExtra(EXTRA_NAME,name)
startActivity(intent);
//2.获取上⼀个Activity传递的值
Intent intent = getIntent();
String name = StringExtra(MainActivity.EXTRA_NAME);
注意:获取数据时,XxxExtra(KEY)⽅法应和所传递的值匹配。
2、以有返回结果的⽅式启动Activity
//1.以有返回结果的⽅式启动下⼀个Activity
private static final int REQUEST_CODE_CHEAT = 0;//请求码
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE_CHEAT );//第⼆个参数为请求码,唯⼀即可
//2.第⼆个Activity中设置返回的值。
public static final String EXTRA_NAME = "name";//键
private String name = "Tom"
Intent intent = new Intent();
intent.putExtra(EXTRA_NAME , name);//存放返回结果的值
setResult(RESULT_OK, intent);//第⼀个参数为结果码,⽤于校验取值
/
/3.在第⼀个Activity中重写onActivityResult⽅法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
//校验结果码是否正确
return;
}
if (requestCode == REQUEST_CODE_CHEAT) {
//校验请求码是否正确
android编程入门指南 pdfif (data == null) {
//判断返回结果是否为空
return;
}
String name = StringExtra(SecondActivity.EXTRA_NAME);//获取返回结果值
Toast.makeText(this,"SecondActivity所传返回值为:" + name, Toast.LENGTH_SHORT).show(); }
}
3、使⽤Serializable⽅式传递对象。
//1.创建实体类并实现接⼝Serializable
public class Person implements Serializable {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
//2.添加传递值的对象
public static final String EXTRA_PERSON= "person";//键
Person person = new Person();//创建对象并赋值
person.setAge(20);
person.setName("Tom");
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(EXTRA_PERSON,person);//以键值对形式存放
startActivity(intent);
//3.获取传递的对象
Person person = (Person) getIntent().getSerializable(MainActivity.EXTRA_PERSON);
4、使⽤Pracelable⽅式传递对象。(较前⼀种推荐这种⽅式传递对象)
//1.创建所传对象实体类并实现Pracelable接⼝。(write和read顺序⼀定要相同) public class Persons implements Parcelable {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);//写出name
dest.writeInt(age);//写出age
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator(){ @Override
public Persons createFromParcel(Parcel source) {
Persons persons = new Persons();
persons.name = adString();//读取name
persons.age = adInt();//读取age
return persons;
}
@Override
public Persons[] newArray(int size) {
return new Persons[size];
}
};
}
//2.添加传递值的对象
public static final String EXTRA_PERSONS= "persons";//键
Persons persons = new Persons();//创建对象并赋值
persons.setAge(20);
persons.setName("Tom");
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(EXTRA_PERSONS,persons);//以键值对形式存放
startActivity(intent);
//3.取值传递的对象
Persons persons = (Persons) getIntent().getParcelableExtra(EXTRA_PERSONS);
5、Android基础之intent传值⽅法和传递⼤量数据⽅案
欢迎指出⽂中不⾜,或提供相关补充。
以上⽅法主要来源于《Android第⼀⾏代码》、《Android编程权威指南》,及本⼈学习整理所得。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。