��ͬ��activity�����Զ������_android_���˲���
http://www.drovik.com/ 2012-9-5 12:41:52 ��Դ��CSDN���� �����
�кܶ�ʱ����Ҫ�ڲ�ͬ��Activity֮�䴫�����ݡ�
ʵ�ַ����кܶ࣬������Bundle�����ݣ�Ҳ�����ø�Intent��putExtra���ݸ��Ӳ�����
��ȻҲ���Դ����Զ�����Ķ���Ϊ�����Զ����������Activity֮�䴫�ݣ��������һЩ���������õķ������Զ�����ʵ��Parcelable�ӿڻ�Serializable�ӿڡ�Android�н���ʵ��Parcelable��
�������������ַ����������ӡ�
����Parcelable��ȥ���ٴ���Serializable������
- import android.os.Parcel;
- import android.os.Parcelable;
- public class WeightParcelable implements Parcelable {
- private String sex;
- private double height;
- //����Ŀչ���������Ϊ������һ��˽�еĹ�������������new����
- public WeightParcelable() {
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public double getHeight() {
- return height;
- }
- public void setHeight(double height) {
- this.height = height;
- }
- @Override
- public int describeContents() {
- return 0;
- }
- //��Ҫ���ǵķ���
- @Override
- public void writeToParcel(Parcel out, int flags) {
- out.writeString(sex);
- out.writeDouble(height);
- }
- //�ؼ�������
- public static final Parcelable.Creator<WeightParcelable> CREATOR = new Parcelable.Creator<WeightParcelable>() {
- public WeightParcelable createFromParcel(Parcel in) {
- return new WeightParcelable(in);
- }
- public WeightParcelable[] newArray(int size) {
- return new WeightParcelable[size];
- }
- };
- //
- private WeightParcelable(Parcel in) {
- sex = in.readString();
- height = in.readDouble();
- }
- }
HeightSerializable.java
- import java.io.Serializable;
- public class HeightSerializable implements Serializable {
- private static final long serialVersionUID = 8502706820090766507L;
- private String sex;
- private double weight;
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public double getWeight() {
- return weight;
- }
- public void setWeight(double weight) {
- this.weight = weight;
- }
- }
EX03_10.java
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.TextView;
- public class EX03_10 extends Activity {
- public static final String parcelableKey = "irdc.ex03_10.parcelableKey";
- public static final String seralizableKey = "irdc.ex03_10.seralizableKey";
- private static final String TAG = "EX03_10";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button b1 = (Button) findViewById(R.id.button1);
- b1.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- EditText et = (EditText) findViewById(R.id.height);
- double height = Double.parseDouble(et.getText().toString());
- String sex = "";
- RadioButton rb1 = (RadioButton) findViewById(R.id.sex1);
- if (rb1.isChecked()) {
- sex = "M";
- } else {
- sex = "F";
- }
- Intent intent = new Intent();
- intent.setClass(EX03_10.this, EX03_10_1.class);
- Bundle bundle = new Bundle();
- WeightParcelable wp = new WeightParcelable();
- wp.setSex(sex);
- wp.setHeight((int) height);
- bundle.putParcelable(parcelableKey, wp);
- intent.putExtras(bundle);
- //ע������
- startActivityForResult(intent, 1);
- }
- });
- }
- //����ǹؼ�
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- Log.d(TAG, "onActivityResult requestCode=" + requestCode
- + " resultCode= " + resultCode + " data == null["
- + (data == null) + "]");
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == 1) {
- if (resultCode == RESULT_OK) {
- try {
- HeightSerializable hs = (HeightSerializable) data
- .getSerializableExtra(seralizableKey);
- TextView textView4 = (TextView) findViewById(R.id.text4);
- textView4.setText("�Ա�" + hs.getSex() + "\n������:"
- + hs.getWeight());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
EX03_10_1.java
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.TextView;
- public class EX03_10_1 extends Activity {
- private static final String TAG = "EX03_10_1";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.myalyout);
- WeightParcelable wp = (WeightParcelable) getIntent()
- .getParcelableExtra(EX03_10.parcelableKey);
- String sex = wp.getSex();
- double height = wp.getHeight();
- String sexText = "";
- if (sex.equals("M")) {
- sexText = "����";
- } else {
- sexText = "��";
- }
- String weight = this.getWeight(sex, height);
- TextView tv1 = (TextView) findViewById(R.id.text1);
- tv1.setText("�Ա�:" + sexText + "\n����:" + height + "����\n������: " + weight
- + "����");
- // ���ض���
- Intent data = new Intent();
- HeightSerializable hs = new HeightSerializable();
- hs.setSex(sexText);
- hs.setWeight(Double.valueOf(weight));
- data.putExtra(EX03_10.seralizableKey, hs);
- //�����ﷵ�ض���
- setResult(RESULT_OK, data);
- }
- private String format(double num) {
- NumberFormat formatter = new DecimalFormat("0.00");
- String s = formatter.format(num);
- return s;
- }
- private String getWeight(String sex, double height) {
- String weight = "";
- if (sex.equals("M")) {
- weight = format((height - 80) * 0.7);
- } else {
- weight = format((height - 70) * 0.6);
- }
- return weight;
- }
- }
��������(0)