在activity之间传递数据
一、简介
二、通过intent传递数据
1、在需要传数据的界面调用
intent.putExtra("data1", "我是fry");方法
//前面是键,后面是值,这个是string
2、在需要接受数据的界面调用bundle获取传递过来的数据即可
Bundle bundle = getIntent().getExtras();
String data1=bundle.getString("data1");
如果是string就getString(),int就getString(),什么数据类型的方法都有
3、实例
需要传数据的界面调用
intent.putExtra("data1", "我是fry");
需要接受数据的界面
Bundle bundle = getIntent().getExtras();
String data1=bundle.getString("data1");
data1里面的内容就是:"我是fry"
三、实例
结果图
原理:
在MainActivity页面创建数据,然后用intent.putExtra方法将数据传递给Activity01页面
代码
activityDataSend.MainActivity
1 package activityDataSend; 2 3 4 5 6 7 import com.example.activityDataSend.R; 8 9 import android.app.Activity; 10 import android.content.Intent; 11 import android.content.Loader; 12 import android.os.Bundle; 13 import android.view.View; 14 import android.view.View.OnClickListener; 15 import android.widget.Button; 16 17 18 19 public class MainActivity extends Activity{ 20 private Button btn_openActivty;//创建一个button对象 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState);//父类操作 23 setContentView(R.layout.activity_main);//引入名为activity_main的界面 24 btn_openActivty=(Button) findViewById(R.id.btn_openActivity);//找id为btn_openActivity的button 25 btn_openActivty.setOnClickListener(new OnClickListener() {//设置button点击监听 26 27 @Override 28 public void onClick(View v) {//onclick事件 29 // TODO Auto-generated method stub 30 Intent intent=new Intent();//初始化intent 31 intent.setClass(MainActivity.this,Activity01.class);//连接 32 //下面这句话用于intent在activity之间传输data 33 intent.putExtra("data1", "我是fry");//前面是键,后面是值,这个是string 34 intent.putExtra("data2", 0.2);//这个是double 35 //传递对象,serializable序列化方式 36 testClass1_serialize class1=new testClass1_serialize("fry1",21); 37 intent.putExtra("data3_obj", class1); 38 39 //传递对象,parcelable序列化方式 40 testClass2_parcelable class2=new testClass2_parcelable("fry2",22); 41 intent.putExtra("data4_obj", class2); 42 43 startActivity(intent);//打开activity 44 } 45 }); 46 } 47 }
activityDataSend.Activity01
1 package activityDataSend; 2 3 4 5 import com.example.activityDataSend.R; 6 7 import android.app.Activity; 8 import android.os.Bundle; 9 import android.util.Log; 10 import android.widget.TextClock; 11 import android.widget.TextView; 12 13 public class Activity01 extends Activity{ 14 private TextView textView_ans; 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 // TODO Auto-generated method stub 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity01); 20 21 textView_ans=(TextView) findViewById(R.id.textView_ans); 22 Bundle bundle = getIntent().getExtras(); 23 String data1=bundle.getString("data1"); 24 double data2=bundle.getDouble("data2"); 25 testClass1_serialize data3_obj=(testClass1_serialize) bundle.get("data3_obj"); 26 testClass2_parcelable data4_obj=(testClass2_parcelable) bundle.get("data4_obj"); 27 28 Log.d("MainActivity",bundle.get("data3_obj")+" "+bundle.get("data4_obj")); 29 System.out.println(bundle.get("data3_obj")+" "+bundle.get("data4_obj")); 30 31 textView_ans.setText("data1: "+data1+" data2: "+data2+" data3_obj: "+data3_obj+" data4_obj: "+data4_obj); 32 //textView_ans.setText("data1: "+data1+" data2: "+data2+" data4_obj: "+data4_obj); 33 } 34 }
Serializable序列化方式
activityDataSend.testClass1_serialize
1 package activityDataSend; 2 3 import java.io.Serializable; 4 5 public class testClass1_serialize implements Serializable{ 6 private String name; 7 private int age; 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public int getAge() { 15 return age; 16 } 17 public void setAge(int age) { 18 this.age = age; 19 } 20 public testClass1_serialize(String name, int age) { 21 super(); 22 this.name = name; 23 this.age = age; 24 } 25 public testClass1_serialize() { 26 super(); 27 } 28 @Override 29 public String toString() { 30 return "testClass1_serialize [name=" + name + ", age=" + age + "]"; 31 } 32 33 }
Parcelable序列化方式
序列化差不多要自己写 反序列化也要自己写
activityDataSend.testClass2_parcelable
1 package activityDataSend; 2 3 import android.os.Parcel; 4 import android.os.Parcelable; 5 6 /* 7 * Parcelable序列化方式 8 * 序列化差不多要自己写 9 * 反序列化也要自己写 10 */ 11 public class testClass2_parcelable implements Parcelable{ 12 private String name; 13 private int age; 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public int getAge() { 21 return age; 22 } 23 public void setAge(int age) { 24 this.age = age; 25 } 26 public testClass2_parcelable() { 27 super(); 28 } 29 public testClass2_parcelable(String name, int age) { 30 super(); 31 this.name = name; 32 this.age = age; 33 } 34 35 @Override 36 public String toString() { 37 return "testClass2_parcelable [name=" + name + ", age=" + age + "]"; 38 } 39 @Override 40 public int describeContents() { 41 // TODO Auto-generated method stub 42 return 0; 43 } 44 /* 45 * 序列化 46 * (non-Javadoc) 47 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int) 48 */ 49 @Override 50 public void writeToParcel(Parcel dest, int flags) {//序列化 51 // TODO Auto-generated method stub 52 dest.writeString(name); 53 dest.writeInt(age); 54 } 55 /* 56 * 反序列化要自己写 57 */ 58 public static Parcelable.Creator<testClass2_parcelable> CREATOR=new Creator<testClass2_parcelable>() { 59 60 @Override 61 public testClass2_parcelable[] newArray(int size) { 62 // TODO Auto-generated method stub 63 return null; 64 } 65 /* 66 * 反序列化 67 * (non-Javadoc) 68 * @see android.os.Parcelable.Creator#createFromParcel(android.os.Parcel) 69 */ 70 @Override 71 public testClass2_parcelable createFromParcel(Parcel source) {//反序列化 72 // TODO Auto-generated method stub 73 testClass2_parcelable class2=new testClass2_parcelable(); 74 class2.setName(source.readString()); 75 class2.setAge(source.readInt()); 76 return class2; 77 } 78 }; 79 }