Activity之间数据的传递
activity数据之间传递可以用到intent
今天介绍的是利用intent来传递实体类,实体类必须实现Parcelable 接口或者Serializable接口,实现Parcelable 先对来说比较节省内存消耗
Person类
package cn.tedu.intent_extra;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
// 根据Parcel容器,创建Person对象
Person p = new Person();
p.setUsername(source.readString()); // "Mike"
p.setAge(source.readInt()); // 18
p.setGender(source.readString()); // "Male"
return p;
}
@Override
public Person[] newArray(int size) {
// 根据size确定数组
return new Person[size];
}
};
@Override
public int describeContents() {
// 描述内容
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// 确定如何将对象的属性写入到Parcel容器
dest.writeString(username); // "Mike"
dest.writeInt(age); // 18
dest.writeString(gender); // "Male"
}
private String username;
private int age;
private String gender;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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;
}
}
Mainactivity代码
package cn.tedu.intent_extra;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class MainActivity extends Activity implements OnClickListener {
private EditText etUsername;
private EditText etAge;
private RadioButton rbMale;
private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = (EditText) findViewById(R.id.et_username);
etAge = (EditText) findViewById(R.id.et_age);
rbMale = (RadioButton) findViewById(R.id.rb_male);
btnSubmit = (Button) findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// 通过控件获取数据
String username = etUsername.getText().toString().trim();
int age = Integer.parseInt(etAge.getText().toString());
String gender = rbMale.isChecked() ? "男" : "女";
// 将数据封装到Person对象中
Person person = new Person();
person.setUsername(username);
person.setAge(age);
person.setGender(gender);
// // 将数据封装到Intent对象中
// Intent intent = new Intent();
// intent.putExtra("_username", username);
// intent.putExtra("_age", age);
// intent.putExtra("_gender", gender);
// 激活SecondActivity
Intent intent = new Intent();
intent.putExtra("data", person);
intent.setClass(this, SecondActivity.class);
startActivity(intent);
}
}
SecondAcitvity代码
package cn.tedu.intent_extra;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// 获取Intent对象
Intent intent = getIntent();
// 从Intent中获取数据
Person p = intent.getParcelableExtra("data");
// String username = intent.getStringExtra("_username");
// long age = intent.getIntExtra("_age", -1);
// String gender = intent.getStringExtra("_gender");
// 日志
Log.d("tedu", "username -> " + p.getUsername());
Log.d("tedu", "age -> " + p.getAge());
Log.d("tedu", "gender -> " + p.getGender());
}
}
MainActivity布局文件如下
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
tools:ignore="TextFields" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入用户名" />
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_title"
android:layout_below="@+id/tv_title"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tv_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/et_username"
android:layout_below="@+id/et_username"
android:text="请输入您的年龄" />
<EditText
android:id="@+id/et_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_age"
android:layout_below="@+id/tv_age"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/et_age"
android:layout_below="@+id/et_age"
android:text="请选择您的性别" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1" >
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_centerVertical="true"
android:text="提交" />
</RelativeLayout>