发这篇博客主要讲一下Android中Intent中如何传值的几种方法:
1:基本数据类型,包含了Java八种基本数据类型和CharSequece文本
2:八种数据类新对应数组和CharSequece文本数组
3:Bundle传值
4:ArrayList集合
5:Serializable传递对象
6:Parcelable传递对象
在 main.xml 布局文件中添加六个Button控件,分别是六种传值方式。
Activity代码如下:
1 package com.example.transmittingdata; 2 3 import java.util.ArrayList; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 11 /*** 12 * 13 * Intent传值包含 14 * 1:基本数据类型,包含了Java八种基本数据类型和CharSequece文本 15 * 2:八种数据类新对应数组和CharSequece文本数组 16 * 3:Bundle 17 * 4:ArrayList集合 5:Serializable传递对象 18 * 6:Parcelable传递对象 19 * 20 * @author zq 21 * 22 */ 23 public class MainActivity extends Activity implements OnClickListener { 24 25 private String[] str = new String[] { "八种数据类新对应数组和CharSequece文本数组", "123" }; 26 private ArrayList<String> list; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.main); 32 initView(); 33 } 34 35 private void initView() { 36 // TODO Auto-generated method stub 37 findViewById(R.id.button1).setOnClickListener(this); 38 findViewById(R.id.button2).setOnClickListener(this); 39 findViewById(R.id.button3).setOnClickListener(this); 40 findViewById(R.id.button4).setOnClickListener(this); 41 findViewById(R.id.button5).setOnClickListener(this); 42 findViewById(R.id.button6).setOnClickListener(this); 43 list = new ArrayList<String>(); 44 list.add("List集合"); 45 list.add("Value"); 46 47 } 48 49 @Override 50 public void onClick(View v) { 51 // TODO Auto-generated method stub 52 Intent intent = new Intent(MainActivity.this, IntentData.class); 53 switch (v.getId()) { 54 case R.id.button1: 55 intent.putExtra("i", 1); 56 intent.putExtra("key", "基本数据类型,包含了Java八种基本数据类型和CharSequece文本"); 57 break; 58 case R.id.button2: 59 intent.putExtra("i", 2); 60 intent.putExtra("key", str); 61 break; 62 case R.id.button3: 63 Bundle bundle = new Bundle(); 64 bundle.putString("key", "Bundle传值"); 65 intent.putExtra("i", 3); 66 intent.putExtra("bundle", bundle); 67 break; 68 case R.id.button4: 69 intent.putExtra("i", 4); 70 intent.putStringArrayListExtra("key", list); 71 break; 72 case R.id.button5: 73 UserInfo user = new UserInfo(); 74 user.setSex("男"); 75 user.setUserName("白子画"); 76 intent.putExtra("i", 5); 77 intent.putExtra("key", user); 78 break; 79 80 case R.id.button6: 81 intent.putExtra("i", 6); 82 UserBean userBean = new UserBean(); 83 userBean.setSex("女"); 84 userBean.setUserName("花千骨"); 85 intent.putExtra("key", userBean); 86 break; 87 88 default: 89 break; 90 } 91 startActivity(intent); 92 } 93 94 }
接收值的Activity类:
1 public class IntentData extends Activity { 2 3 private TextView text1, text2; 4 private int position = 1; 5 private String data = ""; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 // TODO Auto-generated method stub 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.data); 12 initView(); 13 initData(); 14 15 } 16 17 @SuppressLint("NewApi") 18 private void initData() { 19 // TODO Auto-generated method stub 20 if (getIntent() != null) { 21 position = getIntent().getIntExtra("i", 1); 22 if (position == 1) { 23 data = getIntent().getStringExtra("key"); 24 text1.setText("基本数据类型"); 25 text2.setText(data); 26 return; 27 } 28 if (position == 2) { 29 String[] data1 = getIntent().getStringArrayExtra("key"); 30 text1.setText("数组"); 31 text2.setText(data1[0] + "----" + data1[1]); 32 return; 33 } 34 if (position == 3) { 35 Bundle bundle = getIntent().getBundleExtra("bundle"); 36 text1.setText("Bundle"); 37 text2.setText(bundle.getString("key", "默认为空是的值")); 38 return; 39 } 40 if (position == 4) { 41 ArrayList<String> array; 42 text1.setText("List<object> 集合"); 43 array = getIntent().getStringArrayListExtra("key"); 44 text2.setText(array.get(0)); 45 return; 46 } 47 if (position == 5) { 48 UserInfo user; 49 text1.setText("Serializable传递对象"); 50 user = (UserInfo) getIntent().getSerializableExtra("key"); 51 text2.setText(user.getUserName() + "---" + user.getSex()); 52 return; 53 } 54 if (position == 6) { 55 UserBean userBean; 56 text1.setText("Parcelable传递对象"); 57 userBean = (UserBean) getIntent().getParcelableExtra("key"); 58 text2.setText(userBean.getUserName() + "---" 59 + userBean.getSex()); 60 return; 61 } 62 63 } 64 } 65 66 private void initView() { 67 // TODO Auto-generated method stub 68 text1 = (TextView) findViewById(R.id.textView1); 69 text2 = (TextView) findViewById(R.id.textView2); 70 71 } 72 73 }
Serializable传递对象
Serializable:是序列化的意思,表示将一个对象转换成可储存或可传输的状态,对象进行Serializable序列化之后就可以通过Intent来进行Activity之间的传输了。
1 public class UserInfo implements Serializable{ 2 /** 3 * 4 */ 5 private static final long serialVersionUID = 1L; 6 private String sex; 7 private String userName; 8 9 public UserInfo() { 10 // TODO Auto-generated constructor stub 11 } 12 13 public String getSex() { 14 return sex; 15 } 16 public void setSex(String sex) { 17 this.sex = sex; 18 } 19 public String getUserName() { 20 return userName; 21 } 22 public void setUserName(String userName) { 23 this.userName = userName; 24 } 25 26 27 }
这里面的serialVersionUID需要注意一下,它的作用是序列化和反序列化时保持版本的兼容性,如果你未指定,运行时也会默认生成,在进行反序列化时只有数据和当前类的serialVersionUID相同是才能够正常的反序列化,你不指定serialVersionUID一般情况下也不会出问题,但是如果当前类发生了改变例如删掉了某个成员变量那么当前类的serialVersionUID也会出现改变,之后你对数据进行反序列化就会出现错误,这里我指定为1L,L为Long数据类型。
Parcelable传递对象
Parcelable的序列化原理是将一个对象进行分解,而分解后的每一部分都是Intent所支持的数据类型,因此实现了传递对象的功能。
1 public class UserBean implements Parcelable { 2 3 private String sex; 4 private String userName; 5 6 public UserBean() { 7 // TODO Auto-generated constructor stub 8 } 9 10 public String getSex() { 11 return sex; 12 } 13 14 public void setSex(String sex) { 15 this.sex = sex; 16 } 17 18 public String getUserName() { 19 return userName; 20 } 21 22 public void setUserName(String userName) { 23 this.userName = userName; 24 } 25 26 @Override 27 public int describeContents() { 28 // TODO Auto-generated method stub 29 return 0; 30 } 31 32 @Override 33 public void writeToParcel(Parcel dest, int flags) { 34 // TODO Auto-generated method stub 35 36 dest.writeString(userName); 37 dest.writeString(sex); 38 } 39 40 protected UserBean(Parcel in) { 41 userName = in.readString(); 42 sex = in.readString(); 43 } 44 45 public static final Creator<UserBean> CREATOR = new Creator<UserBean>() { 46 47 @Override 48 public UserBean createFromParcel(Parcel in) { 49 return new UserBean(in); 50 } 51 52 @Override 53 public UserBean[] newArray(int size) { 54 return new UserBean[size]; 55 } 56 57 };
可以看到通过Parcelable的实现方式是要复杂很多的,实现Parcelable接口后,需要重写writeToParcel和describeContents方法,describeContents方法直接返回0就可以了,writeToParcel方法我们需要调用Parcel对象进行数据写入,例如dest.writeString(name),注意如果name是字符串类型就调用writeString,如果是Int类型就调用writeInt 等等。