当前位置:首页 > 开发 > 移动开发 > 正文

Intent传递对象的两种实现

发表于: 2013-11-03   作者:come_for_dream   来源:转载   浏览次数:
摘要: MianActivity.java public class IntentDemo extends Activity implements OnClickListener{    /** Called when the activity is first created. */    @Override   

MianActivity.java


public class IntentDemo extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn_01 = (Button) findViewById(R.id.btn_01);
        Button btn_02 = (Button) findViewById(R.id.btn_02);
       
    }

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  switch(v.getId()){
  case R.id.btn_01:{
 
   Intent intent = new Intent(this,PersonView.class);
   Person mPerson = new Person();
   mPerson.setAge(20);
   mPerson.setName("moon");
   Bundle bundle = new Bundle();
   bundle.putSerializable("person", mPerson);
   intent.putExtras(bundle);
   startActivity(intent);
   break;
  }
  
  case R.id.btn_02:{
   Intent intent = new Intent(this,BookView.class);
   Book book = new Book();
   book.setName("manmonth");
   book.setTime("1975");
   book.setAuthor("Brooks");
   Bundle bundle = new Bundle();
   bundle.putParcelable("book", book);
   intent.putExtras(bundle);
   startActivity(intent);
   break;
  }
  
  }
 }
}

Book.java:


public class Book implements Parcelable{

 private String name;
 private String author;
 private String time;

 public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>(){

  @Override
  public Book createFromParcel(Parcel source) {
   // TODO Auto-generated method stub
   Book mBook = new Book();
   mBook.name   = source.readString();
   mBook.time   = source.readString();
   mBook.author = source.readString();
   return mBook;
  }

  @Override
  public Book[] newArray(int size) {
   // TODO Auto-generated method stub
   return new Book[size];
  }
 
 };
 
 @Override
 public int describeContents() {
  // TODO Auto-generated method stub
  return 0;
 }

 @Override
 public void writeToParcel(Parcel dest, int flags) {
  // TODO Auto-generated method stub
  dest.writeString(name);
  dest.writeString(author);
  dest.writeString(time);
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getTime() {
  return time;
 }

 public void setTime(String time) {
  this.time = time;
 }

 public String getAuthor() {
  return author;
 }

 public void setAuthor(String author) {
  this.author = author;
 }
 
}
 

Person.java:

public class Person implements Serializable{
 
 private static final long serialVersionUID = 1L;
 
 private String name;
 private int    age;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
}

 


public class PersonView extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  TextView text = new TextView(this);
  Person person = (Person)getIntent().getSerializableExtra("person");
  text.setText("name:"+person.getName()+"/nage:"+person.getAge()+"/n");
  setContentView(text);
 }
}
 

BookView:

package cn.edu.wtu;


public class BookView extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  TextView text = new TextView(this);
  Book book = (Book) getIntent().getParcelableExtra("book");
  text.setText("name:"+book.getName()+"/nautor:"+book.getAuthor()+"/ntime:"+book.getTime());
  setContentView(text);
 }

}

 

出自http://blog.csdn.net/fuuckwtu/article/details/6522098

Intent传递对象的两种实现

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Android Intent中如何传递对象,就我目前所知道的有两种方法: 一种是Bundle.putSerializable(Key,O
Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Obj
通过Intent,一个activity可以很容易的启动另一个activity,并且传递相关的数据给activity,通过Int
PS:今天上午,非常郁闷,有很多简略基础的问题搞得我有些迷茫,哎,代码几天不写就忘。现在又不当CO
转载自 http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent
Android中Intent传递对象的两种方法(Serializable,Parcelable) 转载自 http://blog.csdn.net/xyz_lm
Android中Intent传递对象的两种方法(Serializable,Parcelable) 转载自 http://blog.csdn.net/xyz_lm
大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法
Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法。 被
以前做项目的时候,需要Intent传递复杂类型,例如:数组,ArrayList类型,传递类对象,今天抽空写了
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号