�����ڵ�λ�ã���ҳ > ���� > Android���� > ����
��ͬ��activity�����Զ������_android_���˲���
http://www.drovik.com/      2012-9-5 12:41:52      ��Դ��CSDN����      �����

�кܶ�ʱ����Ҫ�ڲ�ͬ��Activity֮�䴫�����ݡ�
ʵ�ַ����кܶ࣬������Bundle�����ݣ�Ҳ�����ø�Intent��putExtra���ݸ��Ӳ�����
��ȻҲ���Դ����Զ�����Ķ���Ϊ�����Զ����������Activity֮�䴫�ݣ��������һЩ���������õķ������Զ�����ʵ��Parcelable�ӿڻ�Serializable�ӿڡ�Android�н���ʵ��Parcelable��
�������������ַ����������ӡ�
����Parcelable��ȥ���ٴ���Serializable������

  1. import android.os.Parcel;   
  2. import android.os.Parcelable;   
  3.   
  4. public class WeightParcelable implements Parcelable {   
  5.     private String sex;   
  6.     private double height;   
  7. //����Ŀչ���������Ϊ������һ��˽�еĹ�������������new����   
  8.     public WeightParcelable() {   
  9.   
  10.     }   
  11.   
  12.     public String getSex() {   
  13.         return sex;   
  14.     }   
  15.   
  16.     public void setSex(String sex) {   
  17.         this.sex = sex;   
  18.     }   
  19.   
  20.     public double getHeight() {   
  21.         return height;   
  22.     }   
  23.   
  24.     public void setHeight(double height) {   
  25.         this.height = height;   
  26.     }   
  27.   
  28.     @Override  
  29.     public int describeContents() {   
  30.         return 0;   
  31.     }   
  32. //��Ҫ���ǵķ���   
  33.     @Override  
  34.     public void writeToParcel(Parcel out, int flags) {   
  35.         out.writeString(sex);   
  36.         out.writeDouble(height);   
  37.     }   
  38. //�ؼ�������   
  39.     public static final Parcelable.Creator<WeightParcelable> CREATOR = new Parcelable.Creator<WeightParcelable>() {   
  40.         public WeightParcelable createFromParcel(Parcel in) {   
  41.             return new WeightParcelable(in);   
  42.         }   
  43.   
  44.         public WeightParcelable[] newArray(int size) {   
  45.             return new WeightParcelable[size];   
  46.         }   
  47.     };   
  48. //   
  49.     private WeightParcelable(Parcel in) {   
  50.         sex = in.readString();   
  51.         height = in.readDouble();   
  52.     }   
  53. }  
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

Java���� ���ƴ���
  1. import java.io.Serializable;   
  2.   
  3. public class HeightSerializable implements Serializable {   
  4.     private static final long serialVersionUID = 8502706820090766507L;   
  5.     private String sex;   
  6.     private double weight;   
  7.   
  8.     public String getSex() {   
  9.         return sex;   
  10.     }   
  11.   
  12.     public void setSex(String sex) {   
  13.         this.sex = sex;   
  14.     }   
  15.   
  16.     public double getWeight() {   
  17.         return weight;   
  18.     }   
  19.   
  20.     public void setWeight(double weight) {   
  21.         this.weight = weight;   
  22.     }   
  23.   
  24. }  
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

Java���� ���ƴ���
  1. import android.app.Activity;   
  2. import android.content.Intent;   
  3. import android.os.Bundle;   
  4. import android.util.Log;   
  5. import android.view.View;   
  6. import android.widget.Button;   
  7. import android.widget.EditText;   
  8. import android.widget.RadioButton;   
  9. import android.widget.TextView;   
  10.   
  11. public class EX03_10 extends Activity {   
  12.       
  13.     public static final String parcelableKey = "irdc.ex03_10.parcelableKey";   
  14.     public static final String seralizableKey = "irdc.ex03_10.seralizableKey";   
  15.     private static final String TAG = "EX03_10";   
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {   
  19.         super.onCreate(savedInstanceState);   
  20.         setContentView(R.layout.main);   
  21.   
  22.         Button b1 = (Button) findViewById(R.id.button1);   
  23.         b1.setOnClickListener(new Button.OnClickListener() {   
  24.             public void onClick(View v) {   
  25.                 EditText et = (EditText) findViewById(R.id.height);   
  26.                 double height = Double.parseDouble(et.getText().toString());   
  27.                 String sex = "";   
  28.                 RadioButton rb1 = (RadioButton) findViewById(R.id.sex1);   
  29.                 if (rb1.isChecked()) {   
  30.                     sex = "M";   
  31.                 } else {   
  32.                     sex = "F";   
  33.                 }   
  34.                 Intent intent = new Intent();   
  35.                 intent.setClass(EX03_10.this, EX03_10_1.class);   
  36.   
  37.                 Bundle bundle = new Bundle();   
  38.   
  39.                 WeightParcelable wp = new WeightParcelable();   
  40.                 wp.setSex(sex);   
  41.                 wp.setHeight((int) height);   
  42.   
  43.                 bundle.putParcelable(parcelableKey, wp);   
  44.                 intent.putExtras(bundle);   
  45. //ע������   
  46.                 startActivityForResult(intent, 1);   
  47.             }   
  48.         });   
  49.     }   
  50. //����ǹؼ�   
  51.     @Override  
  52.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
  53.         Log.d(TAG, "onActivityResult requestCode=" + requestCode   
  54.                 + " resultCode= " + resultCode + " data == null["  
  55.                 + (data == null) + "]");   
  56.   
  57.         super.onActivityResult(requestCode, resultCode, data);   
  58.         if (requestCode == 1) {   
  59.             if (resultCode == RESULT_OK) {   
  60.                 try {   
  61.                     HeightSerializable hs = (HeightSerializable) data   
  62.                             .getSerializableExtra(seralizableKey);   
  63.   
  64.                     TextView textView4 = (TextView) findViewById(R.id.text4);   
  65.                     textView4.setText("�Ա�" + hs.getSex() + "\n��׼����:"  
  66.                             + hs.getWeight());   
  67.   
  68.                 } catch (Exception e) {   
  69.                     e.printStackTrace();   
  70.                 }   
  71.             }   
  72.         }   
  73.     }   
  74.   
  75. }  
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

Java����
  1. import java.text.DecimalFormat;   
  2. import java.text.NumberFormat;   
  3.   
  4. import android.app.Activity;   
  5. import android.content.Intent;   
  6. import android.os.Bundle;   
  7. import android.util.Log;   
  8. import android.widget.TextView;   
  9.   
  10. public class EX03_10_1 extends Activity {   
  11.     private static final String TAG = "EX03_10_1";   
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {   
  15.         super.onCreate(savedInstanceState);   
  16.         setContentView(R.layout.myalyout);   
  17.   
  18.         WeightParcelable wp = (WeightParcelable) getIntent()   
  19.                 .getParcelableExtra(EX03_10.parcelableKey);   
  20.   
  21.         String sex = wp.getSex();   
  22.         double height = wp.getHeight();   
  23.   
  24.         String sexText = "";   
  25.         if (sex.equals("M")) {   
  26.             sexText = "����";   
  27.         } else {   
  28.             sexText = "Ů��";   
  29.         }   
  30.   
  31.         String weight = this.getWeight(sex, height);   
  32.   
  33.         TextView tv1 = (TextView) findViewById(R.id.text1);   
  34.         tv1.setText("�Ա�:" + sexText + "\n����:" + height + "����\n��׼����: " + weight   
  35.                 + "����");   
  36.   
  37.         // ���ض���   
  38.         Intent data = new Intent();   
  39.         HeightSerializable hs = new HeightSerializable();   
  40.         hs.setSex(sexText);   
  41.         hs.setWeight(Double.valueOf(weight));   
  42.   
  43.         data.putExtra(EX03_10.seralizableKey, hs);   
  44. //�����ﷵ�ض���   
  45.         setResult(RESULT_OK, data);   
  46.     }   
  47.   
  48.     private String format(double num) {   
  49.         NumberFormat formatter = new DecimalFormat("0.00");   
  50.         String s = formatter.format(num);   
  51.         return s;   
  52.     }   
  53.   
  54.     private String getWeight(String sex, double height) {   
  55.         String weight = "";   
  56.         if (sex.equals("M")) {   
  57.             weight = format((height - 80) * 0.7);   
  58.         } else {   
  59.             weight = format((height - 70) * 0.6);   
  60.         }   
  61.         return weight;   
  62.     }   
��������
��������(0)
���� *
�������� *
��֤�� *ͼƬ�����壿������µõ���֤��