編輯:關於android開發
要求被傳遞的對象必須實現上述2種接口中的一種才能通過Intent直接傳遞
Intent中傳遞這2種對象的方法:
Bundle.putSerializable(Key,Object); //實現Serializable接口的對象 Bundle.putParcelable(Key, Object); //實現Parcelable接口的對象Person.java
package com.hust.bundletest;
import java.io.Serializable;
public class Person implements Serializable {
String name;
String password;
String sex;
public Person(String name, String password, String sex) {
super();
this.name = name;
this.password = password;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
注冊窗體發送Intent的代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//獲得的三個組件
EditText name=(EditText) findViewById(R.id.name);
EditText password=(EditText) findViewById(R.id.password);
RadioButton male=(RadioButton) findViewById(R.id.radio0);
//判斷是否被選
String sex=(male.isChecked())?"男":"女";
//封裝成一個對象
Person p=new Person(name.getText().toString(),password.getText().toString(),sex);
//創建Bundle對象
Bundle bundle=new Bundle();
bundle.putSerializable("person", p);//Bundle中放一個對象
//創建一個Intent對象
Intent intent=new Intent(MainActivity.this,ResultActivity.class);
intent.putExtras(bundle);
//啟動intent對應的Activity
startActivity(intent);
}
});
接收端的代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
//獲取顯示組件
TextView name=(TextView) findViewById(R.id.text1);
TextView password=(TextView) findViewById(R.id.text2);
TextView sex=(TextView) findViewById(R.id.text3);
//獲取Intent對象
Intent intent=getIntent();
//從Intent對象中獲取序列數據
//Person p=(Person) intent.getSerializableExtra("person");相當於
Bundle bundle=intent.getExtras();//獲取Bundle對象
Person p=(Person) bundle.getSerializable("person");//Bundle對象中獲取可序列化對象
name.setText(p.getName());
password.setText(p.getPassword());
sex.setText(p.getSex());
}
以上就可以實現對象的傳遞。
Android Volley框架的使用,androidvolley框架
Android Volley框架的使用,androidvolley框架在Android開發中,經常要通過HTTP請求訪問網絡。為了使通過HTTP請求訪問網絡的過程更加簡單
使用Vitamio打造自己的Android萬能播放器—— 手勢控制亮度、音量、縮放
使用Vitamio打造自己的Android萬能播放器—— 手勢控制亮度、音量、縮放 使用Vitamio打造自己的Android萬能播放器(1)——
Stack Overflow 排錯翻譯,stackoverflow
Stack Overflow 排錯翻譯,stackoverflowStack Overflow 排錯翻譯 - Closing AlertDialog.Buil
Android SurfaceView的生命周期,androidsurfaceview
Android SurfaceView的生命周期,androidsurfaceview本文利用SurfaceView來實現視頻的播放 本文地址:http://www.cn