Activity之间的数据传递

原理图:

Activity之间的数据传递_第1张图片

1)较少数据时

存:

intent.putExtra("username", username);


取:
String username = intent.getStringExtra("username");


2)较多数据时

存:

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)两个Activity之间传递对象

存:

Person person = new Person(2,"章泽天",21);
intent.putExtra("person", person);

取:

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


完整的代码如下:

1)main.xml

<?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="vertical" >
    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="用户名"
        />
    
    <EditText 
        android:id="@+id/et_username"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="密码"
        />
    
    <EditText 
        android:id="@+id/et_password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="进入"
        android:onClick="enter"
        />
</LinearLayout>

2)MainActivity

package com.njupt.passdata;

import com.njupt.domain.Person;

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 v){
		
		String username = et_username.getText().toString();
		String password = et_password.getText().toString();
		
		Intent intent = new Intent(this,Main2Activity.class);
		
//		intent.putExtra("username", username);
//		intent.putExtra("password", password);
		
		Bundle bundle = new Bundle();
		bundle.putString("username",username);
		bundle.putString("password", password);
		intent.putExtras(bundle);
		
		Person person = new Person(2,"章泽天",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;
	}

}

3)main2.xml

<?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="vertical" >
    
    <TextView 
        android:id="@+id/tv_info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

4)Main2Activity

package com.njupt.passdata;

import com.njupt.domain.Person;

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

public class Main2Activity extends Activity {

	private TextView tv_info;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    
		setContentView(R.layout.main2);
		
		Intent intent = getIntent();
	    
//		String username = intent.getStringExtra("username");
//	    String password = intent.getStringExtra("password");
	    
		Bundle bundle = intent.getExtras();
		String username = bundle.getString("username");
		String password = bundle.getString("password");
		
		Person person = intent.getParcelableExtra("person");
		
	    tv_info = (TextView) findViewById(R.id.tv_info);
	    tv_info.setText("用户名: " + username +" , 密码: " + password+"\n"+person.toString());
//	    tv_info.setText(person.toString());
	}
}

5)AndroidManifest.xml

最后一定要记得在清单文件中注册上Main2Activity

<activity android:name="com.njupt.passdata.Main2Activity" android:label="activity之间传递数据"></activity>

6)Person

这个类的写法参考android开发文档

package com.njupt.domain;

import android.os.Parcel;
import android.os.Parcelable;

public class Person implements Parcelable {

	private int id;
	private String name;
	private int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	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;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

	public Person(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public Person(Parcel in) {
		id = in.readInt();
		name = in.readString();
		age = in.readInt();
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeString(name);
		dest.writeInt(age);
	}

	public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
		public Person createFromParcel(Parcel in) {
			return new Person(in);
		}

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


你可能感兴趣的:(Activity之间的数据传递)