activity之间使用Intent传值
2013-10-07 21:54
281 查看
在两个activity之间使用Intent对象进行传值,可以使用Intent对象的putExtra方法,该方法有许多的重载方法对于基本数据类型以及数组的传值,都能满足,这不是本文的重点,现在我们来探讨一下使用Parcelable和Serializable方式传递对象
前提条件:使用TestActivity1和TestActivity2两个activity进行传值,其中TestActivity1是值的设置方,TestActivity2是值的接收方,传递的对象是TestUser1
TestUser1对象如下:
TestActivity1代码如下:
布局文件test1如下:
界面如下:
点击“跳转到test2”按钮能够将对象传递到TestActivity2中
TestActivity代码如下:
效果如下:
我们看到testname已经完整的传值到了TestActivity2里面
TestUser2对象如下:
TestActivity1代码如下:
TestActivity2代码如下:
效果如下:
其中testname2 和16分别是username和age的值
注意:writeToParcel方法中的顺序要和TestUser2(Parcel
p)构造函数的顺序和数量要一致
Serializable方式:
Serializable方式实际上是将对象进行序列化后进行传递,所以在我们传递对象时候,一定要记得实现Serializable接口生成serialVersionUID,这样才能在使用Intent.putExtra()方法,例子如下:前提条件:使用TestActivity1和TestActivity2两个activity进行传值,其中TestActivity1是值的设置方,TestActivity2是值的接收方,传递的对象是TestUser1
TestUser1对象如下:
public class TestUser1 implements Serializable{ private static final long serialVersionUID = -6845458138306849582L; private String username; private String psw; public TestUser1() { super(); } public TestUser1(String username, String psw) { super(); this.username = username; this.psw = psw; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } public static long getSerialversionuid() { return serialVersionUID; } }
TestActivity1代码如下:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test1); test1_btn = (Button)findViewById(R.id.test1_btn); test1_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(TestActivity1.this,TestActivity2.class); /* * 方式一:Serializable */ TestUser1 user = new TestUser1(); user.setPsw("123456"); user.setUsername("testname"); intent.putExtra("user", user); startActivity(intent); } }); }
布局文件test1如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/test1_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转到test2" /> <EditText android:id="@+id/test1_text" android:layout_width="150dp" android:layout_height="wrap_content" android:hint="hint text" /> </LinearLayout>
界面如下:
点击“跳转到test2”按钮能够将对象传递到TestActivity2中
TestActivity代码如下:
public class TestActivity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test2); Intent intent = getIntent(); EditText test2_text = (EditText)findViewById(R.id.test2_text); /* * Serializable方式 */ TestUser1 user = (TestUser1)intent.getSerializableExtra("user"); test2_text.setText(user.getUsername()); } }
效果如下:
我们看到testname已经完整的传值到了TestActivity2里面
Parcelable方式:
前提条件:使用TestActivity1和TestActivity2两个activity进行传值,其中TestActivity1是值的设置方,TestActivity2是值的接收方,传递的对象是TestUser2TestUser2对象如下:
public class TestUser2 implements Parcelable { private String username; private String psw; private int age; public TestUser2() { super(); } public TestUser2(String username, String psw,int age) { super(); this.username = username; this.psw = psw; this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public TestUser2(Parcel p) { username = p.readString(); // psw = p.readString(); age = p.readInt(); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } public static final Parcelable.Creator<TestUser2> CREATOR = new Parcelable.Creator<TestUser2>() { public TestUser2 createFromParcel(Parcel p) { return new TestUser2(p); } public TestUser2[] newArray(int size) { return new TestUser2[size]; } }; @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { /* * 此处的顺序与TestUser2(p)构造函数中的顺序以及个数要一致,如果不一致会出现问题 */ dest.writeString(username); // dest.writeString(psw); dest.writeInt(age); } }
TestActivity1代码如下:
public class TestActivity1 extends Activity { private Button test1_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test1); test1_btn = (Button)findViewById(R.id.test1_btn); test1_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(TestActivity1.this,TestActivity2.class); /* * 方式二 */ TestUser2 user2 = new TestUser2(); user2.setAge(16); user2.setUsername("testname2"); intent.putExtra("user2", user2); startActivity(intent); } }); } }
TestActivity2代码如下:
public class TestActivity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test2); Intent intent = getIntent(); EditText test2_text = (EditText)findViewById(R.id.test2_text); /* * Parcelable方式 */ TestUser2 user2 = (TestUser2)intent.getParcelableExtra("user2"); test2_text.setText(user2.getUsername()+user2.getAge()); } }
效果如下:
其中testname2 和16分别是username和age的值
注意:writeToParcel方法中的顺序要和TestUser2(Parcel
p)构造函数的顺序和数量要一致
相关文章推荐
- iOS的三种多线程技术:
- 浏览器小览【欢迎讨论】
- HTTP协议详解
- 6410的存储空间分配
- oracle的nvl函数的使用解析
- 七天美音英标学习总结
- 做个标记!
- Linux 系统调用
- hdu 3853 LOOPS(基础DP求期望)
- 实习心得体会--在一家互联网公司4个月的心得体会
- 九度online judge 1543 二叉树
- 《明末农民战争史》:出版于30年前,至今仍是李自成张献忠起义的权威著作
- HTTP协议详解(真的很经典)
- 软考(7)——看图心想 标准化和知识产权
- 1007
- 我的samba配置
- 开篇有益:为什么选择MongoDB?
- FTP 的主动方式与被动方式
- 2013年第三季度总结报告
- 二级指针