Intent 传数据

Intent作为android重要的组件其重要性不言而喻,这里说说他是怎么传递简单数据和对象

    Intent的具体概念就不讲解了!网上有很多的

  • 传递简单的数据(例如String,float等)
  • 传递对象(利用Serializable接口) 
  • 传递对象(利用Parcelable接口)

  传对象时候至于选取哪种可参考下面的原则:

    1.在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。
    2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
    3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable

    下面就通过一个简单的例子来进行传值:

  MainActivity包含了三个按钮分别用来传递简单数据和对象和另外的对象。XML文件就是三个Button就不贴出来了

  

 1 public class MainActivity extends Activity implements OnClickListener {
 2 
 3     Button btn1 = null;
 4     Button btn2 = null;
 5     Button btn3 = null;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11 
12         btn1 = (Button) findViewById(R.id.button1);
13         btn2 = (Button) findViewById(R.id.button2);
14         btn3 = (Button) findViewById(R.id.button3);
15 
16         btn1.setOnClickListener(this);
17         btn2.setOnClickListener(this);
18         btn3.setOnClickListener(this);
19     }
20 
21     @Override
22     public boolean onCreateOptionsMenu(Menu menu) {
23         getMenuInflater().inflate(R.menu.main, menu);
24         return true;
25     }
26 
27     @Override
28     public void onClick(View v) {
29         switch (v.getId()) {
30         case R.id.button1:
31             Intent intent1 = new Intent();
32             intent1.putExtra("name", "西瓜");
33             intent1.putExtra("color", "红加绿");
34             intent1.putExtra("price", 10);
35             intent1.setClass(MainActivity.this, MessageActivity.class);
36             startActivity(intent1);
37             break;
38         case R.id.button2:
39             Intent intent2 = new Intent();
40             Bundle bundle = new Bundle();
41             apple ap = new apple();
42             ap.setColor("红色");
43             ap.setName("苹果");
44             ap.setPrice(15);
45             bundle.putSerializable("fruit", ap);
46             intent2.putExtras(bundle);
47             intent2.setClass(MainActivity.this, AppleActivity.class);
48             startActivity(intent2);
49             break;
50         case R.id.button3:
51             Intent intent3 = new Intent();
52             Bundle bundle2 = new Bundle();
53             orange or = new orange();
54             or.setFruitColor("橘黄色");
55             or.setName("橘子");
56             or.setPrice(12);
57             bundle2.putParcelable("orange", or);
58             intent3.putExtras(bundle2);
59             intent3.setClass(MainActivity.this, OrangeActivity.class);
60             startActivity(intent3);
61             break;
62         default:
63             break;
64 
65         }
66 
67     }
68 
69 }
View Code

 


  每个Button转向的Activity

  简单数据的Activity是MessageActivity,利用Serializable传对的Activity是AppleActivity ,利用Parcelable传对的Activity是OrangeActivity

  他们XML文件当中都是有三个TextView 就不贴出来了

    MessageActivity:

 1 public class MessageActivity extends Activity {
 2 
 3     TextView text1 = null;
 4     TextView text2 = null;
 5     TextView text3 = null;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_message);
11 
12         text1 = (TextView) findViewById(R.id.Name);
13         text2 = (TextView) findViewById(R.id.Price);
14         text3 = (TextView) findViewById(R.id.Color);
15         
16         Intent intent = getIntent();
17         text1.setText(intent.getStringExtra("name"));
18         text2.setText(intent.getFloatExtra("price", 5)+"");
19         text3.setText(intent.getStringExtra("color"));
20         
21         
22     }
23 
24     @Override
25     public boolean onCreateOptionsMenu(Menu menu) {
26         getMenuInflater().inflate(R.menu.message, menu);
27         return true;
28     }
29 
30 }
View Code

    AppleActivity:

 1 public class AppleActivity extends Activity {
 2 
 3     TextView text1 = null;
 4     TextView text2 = null;
 5     TextView text3 = null;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_apple);
11         text1 = (TextView) findViewById(R.id.appleName);
12         text2 = (TextView) findViewById(R.id.applePrice);
13         text3 = (TextView) findViewById(R.id.appleColor);
14 
15         apple ap = (apple) getIntent().getSerializableExtra("fruit");
16         text1.setText(ap.getName());
17         text2.setText(ap.getPrice() + "");
18         text3.setText(ap.getColor());
19     }
20 
21     @Override
22     public boolean onCreateOptionsMenu(Menu menu) {
23         getMenuInflater().inflate(R.menu.apple, menu);
24         return true;
25     }
26 
27 }
View Code

    OrangeActivity:

 1 public class OrangeActivity extends Activity {
 2 
 3     TextView text1 = null;
 4     TextView text2 = null;
 5     TextView text3 = null;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_orange);
11 
12         text1 = (TextView) findViewById(R.id.orangeName);
13         text2 = (TextView) findViewById(R.id.orangePrice);
14         text3 = (TextView) findViewById(R.id.orangeColor);
15 
16         orange or = (orange) getIntent().getParcelableExtra("orange");
17 
18         text1.setText(or.getName());
19         text2.setText(or.getPrice() + "");
20         text3.setText(or.getFruitColor());
21 
22     }
23 
24     @Override
25     public boolean onCreateOptionsMenu(Menu menu) {
26         getMenuInflater().inflate(R.menu.orange, menu);
27         return true;
28     }
29 
30 }
View Code

  继承Serializable来创建实体类:

  1 public class apple implements Serializable {
  2 
  3     public apple() {
  4     }
  5 
  6     private String color;
  7     private String name;
  8     private float price;
  9 
 10     /**
 11      * @return the color
 12      */
 13     public String getColor() {
 14         return color;
 15     }
 16 
 17     /**
 18      * @param color
 19      *            the color to set
 20      */
 21     public void setColor(String color) {
 22         this.color = color;
 23     }
 24 
 25     /**
 26      * @return the name
 27      */
 28     public String getName() {
 29         return name;
 30     }
 31 
 32     /**
 33      * @param name
 34      *            the name to set
 35      */
 36     public void setName(String name) {
 37         this.name = name;
 38     }
 39 
 40     /**
 41      * @return the price
 42      */
 43     public float getPrice() {
 44         return price;
 45     }
 46 
 47     /**
 48      * @param price
 49      *            the price to set
 50      */
 51     public void setPrice(float price) {
 52         this.price = price;
 53     }
 54 
 55     /*
 56      * (non-Javadoc)
 57      * 
 58      * @see java.lang.Object#toString()
 59      */
 60     @Override
 61     public String toString() {
 62         return "apple [color=" + color + ", name=" + name + "]";
 63     }
 64 
 65     /*
 66      * (non-Javadoc)
 67      * 
 68      * @see java.lang.Object#hashCode()
 69      */
 70     @Override
 71     public int hashCode() {
 72         final int prime = 31;
 73         int result = 1;
 74         result = prime * result + ((color == null) ? 0 : color.hashCode());
 75         result = prime * result + ((name == null) ? 0 : name.hashCode());
 76         return result;
 77     }
 78 
 79     /*
 80      * (non-Javadoc)
 81      * 
 82      * @see java.lang.Object#equals(java.lang.Object)
 83      */
 84     @Override
 85     public boolean equals(Object obj) {
 86         if (this == obj)
 87             return true;
 88         if (obj == null)
 89             return false;
 90         if (getClass() != obj.getClass())
 91             return false;
 92         apple other = (apple) obj;
 93         if (color == null) {
 94             if (other.color != null)
 95                 return false;
 96         } else if (!color.equals(other.color))
 97             return false;
 98         if (name == null) {
 99             if (other.name != null)
100                 return false;
101         } else if (!name.equals(other.name))
102             return false;
103         return true;
104     }
105 
106 }
View Code

  继承Parcelable来创建实体类:  在继承Parcelor创建实体类的时候一定要复杂一些,要注意CREATOR 一定要大写,写小写会报错

  1 public class orange implements Parcelable {
  2 
  3     private String fruitColor;
  4     private String name;
  5     private float price;
  6 
  7     public orange() {
  8     }
  9 
 10     public orange(String fruitColor, String name, float price) {
 11         this.fruitColor = fruitColor;
 12         this.name = name;
 13         this.price = price;
 14     }
 15 
 16     @Override
 17     public int describeContents() {
 18         return 0;
 19     }
 20 
 21     // 将实体的属性,也就是成员变量保存到parcel当中去
 22     @Override
 23     public void writeToParcel(Parcel parcel, int flags) {
 24         parcel.writeString(fruitColor);
 25         parcel.writeString(name);
 26         parcel.writeFloat(price);
 27     }
 28 
 29     public static final Parcelable.Creator<orange> CREATOR = new Creator<orange>() {
 30 
 31         @Override
 32         public orange[] newArray(int size) {
 33             return new orange[size];
 34         }
 35 
 36         // 从parcel当中恢复实体
 37         @Override
 38         public orange createFromParcel(Parcel parcel) {
 39             // 注意要和成员变量顺序一样
 40             orange or = new orange();
 41             or.setFruitColor(parcel.readString());
 42             or.setName(parcel.readString());
 43             or.setPrice(parcel.readFloat());
 44             return or;
 45         }
 46     };
 47 
 48     /**
 49      * @return the fruitColor
 50      */
 51     public String getFruitColor() {
 52         return fruitColor;
 53     }
 54 
 55     /**
 56      * @param fruitColor
 57      *            the fruitColor to set
 58      */
 59     public void setFruitColor(String fruitColor) {
 60         this.fruitColor = fruitColor;
 61     }
 62 
 63     /**
 64      * @return the name
 65      */
 66     public String getName() {
 67         return name;
 68     }
 69 
 70     /**
 71      * @param name
 72      *            the name to set
 73      */
 74     public void setName(String name) {
 75         this.name = name;
 76     }
 77 
 78     /**
 79      * @return the price
 80      */
 81     public float getPrice() {
 82         return price;
 83     }
 84 
 85     /**
 86      * @param price
 87      *            the price to set
 88      */
 89     public void setPrice(float price) {
 90         this.price = price;
 91     }
 92 
 93     /*
 94      * (non-Javadoc)
 95      * 
 96      * @see java.lang.Object#toString()
 97      */
 98     @Override
 99     public String toString() {
100         return "orange [fruitColor=" + fruitColor + ", name=" + name + "]";
101     }
102 
103     /*
104      * (non-Javadoc)
105      * 
106      * @see java.lang.Object#hashCode()
107      */
108     @Override
109     public int hashCode() {
110         final int prime = 31;
111         int result = 1;
112         result = prime * result
113                 + ((fruitColor == null) ? 0 : fruitColor.hashCode());
114         result = prime * result + ((name == null) ? 0 : name.hashCode());
115         return result;
116     }
117 
118     /*
119      * (non-Javadoc)
120      * 
121      * @see java.lang.Object#equals(java.lang.Object)
122      */
123     @Override
124     public boolean equals(Object obj) {
125         if (this == obj)
126             return true;
127         if (obj == null)
128             return false;
129         if (getClass() != obj.getClass())
130             return false;
131         orange other = (orange) obj;
132         if (fruitColor == null) {
133             if (other.fruitColor != null)
134                 return false;
135         } else if (!fruitColor.equals(other.fruitColor))
136             return false;
137         if (name == null) {
138             if (other.name != null)
139                 return false;
140         } else if (!name.equals(other.name))
141             return false;
142         return true;
143     }
144 
145 }
View Code

     运行后的效果:

     

 

 

 

 

  

posted @ 2014-04-10 23:45  perfect亮  阅读(282)  评论(0编辑  收藏  举报