(Android review)Activity之间的数据传递


一、基本知识点

1、Activity之间传递数据
1)传递基本类型或String
intent.putExtra("username", username);
 
 getIntent();
 intent.getStringExtra("username");


2)以bundle的形式传

    Bundle bundle = new Bundle();
    bundle.putString("username", username);
    bundle.putString("password", password);
   
    intent.putExtras(bundle);




Bundle bundle = intent.getExtras();
String username = bundle.getString("username");
String password = bundle.getString("password");


3)传递自定义类型


1))implements Parcelable
2))重写以下两个方法
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}

//把数据写入到Parcel对象
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeInt(id);
dest.writeString(name);
dest.writeInt(age);
}


3))拷贝以下方法(把Person换成你的实际类型)


 public static final Parcelable.Creator<Person> CREATOR
   = new Parcelable.Creator<Person>() {
    //从Parcel对象里面读取数据
public Person createFromParcel(Parcel in) {
   return new Person(in);
}

public Person[] newArray(int size) {
   return new Person[size];
}
};

public Person(Parcel in) {
// TODO Auto-generated constructor stub
id = in.readInt();
name = in.readString();
age = in.readInt();
}




Person person = new Person(100, "张信哲", 40);
    intent.putExtra("person", person);




Person person = intent.getParcelableExtra("person");


二、代码

1、MainActivity

package com.example.parsedata;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

	private EditText et_username;
	private EditText et_password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		et_username = (EditText) findViewById(R.id.et_username);
		et_password = (EditText) findViewById(R.id.et_password);
	}

	public void enter(View view) {

		String username = et_username.getText().toString();
		String password = et_password.getText().toString();

		Intent intent = new Intent(this, MainActivity2.class);
		intent.putExtra("username", username);
		intent.putExtra("password", password);
		
		
		Bundle bundle = new Bundle();
		bundle.putString("zhangzetian", "huangjundong");
		intent.putExtras(bundle);
		
		Person person = new Person(2, "hjd is a handsomeboy..", 21);
		intent.putExtra("person", person);
		
		
		startActivity(intent);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


2、MainActivity2

package com.example.parsedata;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity2 extends Activity {

	private TextView tv_info;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
		
		tv_info = (TextView) findViewById(R.id.tv_info);
		
		Intent intent = getIntent();
		String username = intent.getStringExtra("username");
		String password = intent.getStringExtra("password");
		
		Bundle bundle = intent.getExtras();
		
		Person p = intent.getParcelableExtra("person");
		
		tv_info.setText("username: " + username + ",password: " + password + "zhangzetian's husband is : " + bundle.getString("zhangzetian") + " person: " + p.toString());
		
		
	}
	
}


三、源码下载:

http://download.csdn.net/detail/caihongshijie6/7792903


四、效果图




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值