码迷,mamicode.com
首页 > 其他好文 > 详细

通过 Intent 传递类对象

时间:2016-03-03 19:36:24      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象一种是通过实现Parcelable接口传递对象

要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递

Intent中传递这2种对象的方法:

Bundle.putSerializable(Key,Object);  //实现Serializable接口的对象
Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象
Person.java

package com.hust.bundletest;

import java.io.Serializable;

public class Person implements Serializable {
    String name;
    String password;
    String sex;
	public Person(String name, String password, String sex) {
		super();
		this.name = name;
		this.password = password;
		this.sex = sex;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
    
}
注册窗体发送Intent的代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn=(Button) findViewById(R.id.button1);
          
        btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//获得的三个组件
				EditText name=(EditText) findViewById(R.id.name);
		        EditText password=(EditText) findViewById(R.id.password);
		        RadioButton male=(RadioButton) findViewById(R.id.radio0);
		        //判断是否被选
		        String sex=(male.isChecked())?"男":"女";
		        //封装成一个对象
		        Person p=new Person(name.getText().toString(),password.getText().toString(),sex);
				//创建Bundle对象
				Bundle bundle=new Bundle();
				bundle.putSerializable("person", p);//Bundle中放一个对象
				//创建一个Intent对象
				Intent intent=new Intent(MainActivity.this,ResultActivity.class);
				intent.putExtras(bundle);
				//启动intent对应的Activity
				startActivity(intent);				
			}
        	
        });
接收端的代码:

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);
		//获取显示组件
		TextView name=(TextView) findViewById(R.id.text1);
		TextView password=(TextView) findViewById(R.id.text2);
		TextView sex=(TextView) findViewById(R.id.text3);
		//获取Intent对象
		Intent intent=getIntent();
		//从Intent对象中获取序列数据
		//Person p=(Person) intent.getSerializableExtra("person");相当于
		
		Bundle bundle=intent.getExtras();//获取Bundle对象
		Person p=(Person) bundle.getSerializable("person");//Bundle对象中获取可序列化对象
		
		name.setText(p.getName());
		password.setText(p.getPassword());
		sex.setText(p.getSex());
			
	}
以上就可以实现对象的传递。
如果传递的是List<Object>,可以把list强转成Serializable类型,而且object类型也必须实现了Serializable接口

Intent.putExtras(key, (Serializable)list) 
(List<YourObject>)getIntent().getSerializable(key)

通过 Intent 传递类对象

标签:

原文地址:http://blog.csdn.net/tuke_tuke/article/details/50789751

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!