Intent 傳遞簡單型別, 陣列, Object物件,與ArrayList 型別資料
阿新 • • 發佈:2019-02-03
Android應用可以通過Intent實現元件間、程序間通訊,從而實現在它們間傳遞資料,包括簡單資料型別,類物件,泛型資料。簡單資料型別可以直接傳遞,而涉及到類物件的複雜資料型別,需要把類物件轉換為基礎的位元組陣列,如資料類實現Serializable或Parcelable介面,達到系列化。
例程:包括了多種資料型別,分別有簡單資料型別String,int[],類物件Object,以及泛型ArrayList<T>
MainActivity截圖(輸入的資料作為構造一個Person物件)
SecondActivity截圖
對應左邊型別顯示的資料都是該型別通過Intent傳遞過來的資料
原始碼:
1.佈局檔案
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/headline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="MainActivity"/> <TextView android:id="@+id/string" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/headline" android:layout_marginLeft="30dp" android:layout_marginTop="34dp" android:text="姓名:"/> <TextView android:id="@+id/array" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/string" android:layout_below="@+id/string" android:layout_marginTop="30dp" android:text="年齡:"/> <TextView android:id="@+id/objser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/array" android:layout_below="@+id/array" android:layout_marginTop="30dp" android:text="性別:"/> <EditText android:id="@+id/et_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/string" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:ems="10"/> <EditText android:id="@+id/et_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/array" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:ems="10" android:inputType="number"/> <EditText android:id="@+id/et_gender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/objser" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:ems="10"/> <Button android:id="@+id/btn_confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/headline" android:layout_centerVertical="true" android:text="確定"/> </RelativeLayout>
activity_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/headline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="SecondActivity"> <requestFocus/> </TextView> <TextView android:id="@+id/string" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/headline" android:layout_marginLeft="30dp" android:layout_marginTop="34dp" android:text="String:"/> <TextView android:id="@+id/array" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/string" android:layout_below="@+id/string" android:layout_marginTop="30dp" android:text="Array:"/> <TextView android:id="@+id/objser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/array" android:layout_below="@+id/array" android:layout_marginTop="30dp" android:text="ObjSer:"/> <TextView android:id="@+id/objpar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/objser" android:layout_below="@+id/objser" android:layout_marginTop="30dp" android:text="ObjPar:"/> <TextView android:id="@+id/listString" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/objpar" android:layout_below="@+id/objpar" android:layout_marginTop="30dp" android:text="ListStr:"/> <TextView android:id="@+id/listObjSer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/listString" android:layout_below="@+id/listString" android:layout_marginTop="30dp" android:text="ListObjSer:"/> <TextView android:id="@+id/listObjPar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/listString" android:layout_below="@+id/listObjSer" android:layout_marginTop="30dp" android:text="ListObjPar:"/> <EditText android:id="@+id/sc_et_string" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/string" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:editable="false" android:ems="10"/> <EditText android:id="@+id/sc_et_array" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/array" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:editable="false" android:ems="10"/> <EditText android:id="@+id/sc_et_objser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/objser" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:editable="false" android:ems="10"/> <EditText android:id="@+id/sc_et_objpar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/objpar" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:editable="false" android:ems="10"/> <EditText android:id="@+id/sc_et_listStr" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/listString" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:editable="false" android:ems="10"/> <EditText android:id="@+id/sc_et_listObjSer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/listObjSer" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:editable="false" android:ems="10"/> <EditText android:id="@+id/sc_et_listObjPar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/listObjPar" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:editable="false" android:ems="10"/> </RelativeLayout>
2.實現系列化介面的類
Person implements Serializable
public class Person implements Serializable {
//與普通類差別的就是添加了這個系列號
private static final long serialVersionUID = 1L;
private String name;
private int age;
private String gender;
public Person(String name, int age, String gender) {
super();
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
Book implements Parcelable
public class Book implements Parcelable {
private String title;
private String author;
private double price;
public Book(String title, String author, double price) {
super();
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//重寫介面描述
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
//重寫方法,將物件寫入parcel中,注意與讀出成員的順序
@Override
public void writeToParcel(Parcel arg0, int arg1) {
// TODO Auto-generated method stub
arg0.writeString(title);
arg0.writeString(author);
arg0.writeDouble(price);
}
//例項化CREATOR,修飾符與變數名不可改
public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() {
//解析parcel並返回物件
@Override
public Book createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new Book(source);
}
@Override
public Book[] newArray(int size) {
// TODO Auto-generated method stub
return new Book[size];
}
};
//從parcel中解析出物件成員
public Book(Parcel source) {
// TODO Auto-generated constructor stub
title = source.readString();
author = source.readString();
price = source.readDouble();
}
}
3.介面邏輯檔案
MainActivity.java
/**
* @author Coderoyal
* 通過Intent傳遞各種型別資料,分別包括Sting,int[],其他常用型別同樣(string,array);
* 還有物件型別,其中包括兩種情況即實現Serializable或Parcelable介面的類(personser,bookpar);
* 以及List型別,其中的成員也包括三種情況即基本型別,實現Serializable或Parcelable的型別(liststr,listobjser,listobjpar)
*/
public class MainActivity extends Activity {
Person person;
Book book;
ArrayList<String> stringList = new ArrayList<String>();
ArrayList<Person> personList = new ArrayList<Person>();
ArrayList<Book> bookList = new ArrayList<Book>();
private int[] valueArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
EditText nameEditText, ageEditText, genderEditText;
Button confirmButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//載入控制元件
nameEditText = (EditText) findViewById(R.id.et_name);
ageEditText = (EditText) findViewById(R.id.et_age);
genderEditText = (EditText) findViewById(R.id.et_gender);
confirmButton = (Button) findViewById(R.id.btn_confirm);
//給ArrayList<String>新增資料
stringList.add("listString0");
stringList.add("listString1");
stringList.add("listString2");
stringList.add("listString3");
//給ArrayList<?extends Serializable>新增資料
personList.add(new Person("Zhangsan", 22, "male"));
personList.add(new Person("Lisi", 23, "female"));
personList.add(new Person("Wangwu", 24, "male"));
//給ArrayList<?extends Parcelable>新增資料
book = new Book("霧", "巴金", 33.00);
bookList.add(book);
bookList.add(new Book("雨", "巴金", 42.00));
bookList.add(new Book("電", "巴金", 27.50));
//確定按鈕監聽從而實現啟動SecondActivity
confirmButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//讀取介面資料並構造一個person物件
int age = Integer.parseInt(ageEditText.getText().toString());
person = new Person(nameEditText.getText().toString(), age,
genderEditText.getText().toString());
//構造啟動SecondActivity的意圖物件intent
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
//給intent新增普通型別資料
//putExtra(String name, boolean value)方法的引數為鍵值對,name為關鍵字,value為想要的數值
intent.putExtra("string", nameEditText.getText().toString());
intent.putExtra("array", valueArray);
intent.putExtra("personser", person);
intent.putExtra("bookpar", book);
intent.putStringArrayListExtra("liststr", stringList);
intent.putExtra("listobjser", (Serializable) personList);
intent.putParcelableArrayListExtra("listobjpar", bookList);
/*
//這裡也可以用另一種方式,先打包資料
Bundle bundle = new Bundle();
bundle.putString("string", nameEditText.getText().toString());
bundle.putIntArray("array", valueArray);
bundle.putSerializable("personser", person);
bundle.putParcelable("bookpar", book);
bundle.putStringArrayList("liststr", stringList);
bundle.putSerializable("listobjser", personList);
bundle.putParcelableArrayList("listobjpar", bookList);
//把包bundle封裝到intent
intent.putExtras(bundle);*/
//把intent傳遞給startActivity()實現啟動SecondActivity
startActivity(intent);
}
});
}
}
SecondActivity.java
public class SecondActivity extends Activity {
EditText stringEditText, arrayEditText, objSerEditText, objParEditText,
listStrEditText, listObjSerEditText, listObjParEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
stringEditText = (EditText) findViewById(R.id.sc_et_string);
arrayEditText = (EditText) findViewById(R.id.sc_et_array);
objSerEditText = (EditText) findViewById(R.id.sc_et_objser);
objParEditText = (EditText) findViewById(R.id.sc_et_objpar);
listStrEditText = (EditText) findViewById(R.id.sc_et_listStr);
listObjSerEditText = (EditText) findViewById(R.id.sc_et_listObjSer);
listObjParEditText = (EditText) findViewById(R.id.sc_et_listObjPar);
//獲取intent物件
Intent intent = getIntent();
//獲取intent攜帶的資料
Bundle bundle = intent.getExtras();
String string = bundle.getString("string");
int[] valueArray = bundle.getIntArray("array");
Person person = (Person) bundle.getSerializable("personser");
Book book1 = (Book) bundle.getParcelable("bookpar");
ArrayList<String> stringList = bundle.getStringArrayList("liststr");
ArrayList<Person> personList = (ArrayList<Person>) bundle
.getSerializable("listobjser");
ArrayList<Book> bookList = bundle.getParcelableArrayList("listobjpar");
//顯示到控制元件
stringEditText.setText(string);
arrayEditText.setText(valueArray[5] + "");
objSerEditText.setText(person.getGender());
objParEditText.setText(new DecimalFormat("##.00").format(book1
.getPrice()));
listStrEditText.setText(stringList.get(2));
listObjSerEditText.setText(personList.get(0).getName());
listObjParEditText.setText(bookList.get(1).getTitle());
}
}
4.最後別忘了在AndroidManifest.xml 檔案中新增Activity的宣告:
<activity android:name=".SecondActivity"/>