<p>
Android中activity间数据传递方式总结了以下四种:采用Intent(意图)对象传递,采用Application全局变量传递,采用剪切板ClipboardManager传递,采用静态变量传递</p><p>第一种:采用Intent对象传递</p>
package com.xp.android.demo;
import java.io.Serializable;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
/*传递普通类型数据*/
intent.putExtra("name", "张三丰");
intent.putExtra("age", 23);
intent.putExtra("addr", "北京");
/*传递对象类型数据*/
Person person = new Person("张三丰", 23, "北京");
Bundle bundle = new Bundle();
bundle.putSerializable("person", person);
intent.putExtra("data", bundle);
startActivity(intent);
}
});
}
}
class Person implements Serializable{
String name;
int age;
String addr;
public Person(){}
public Person(String name, int age, String addr) {
this.name = name;
this.age = age;
this.addr = addr;
}
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 String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String toString() {
return "Person [name=" + name + ", age=" + age + ", addr=" + addr + "]";
}
}
package com.xp.android.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity {
TextView textView1,textView2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
Intent intent = getIntent();
textView1.setText("name:"+intent.getStringExtra("name")
+" age:"+intent.getIntExtra("age", 0)
+" addr:"+intent.getStringExtra("addr"));
Person person = (Person) intent.getBundleExtra("data").getSerializable("person");
textView2.setText("name:"+person.getName()
+" age:"+person.getAge()
+" addr:"+person.getAddr());
}
}
第二种:采用全局变量()
</pre><pre name="code" class="java">MainActivity中的button的点击事件
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
MyApp app = (MyApp) getApplication();
app.setAge(100);
startActivity(intent);
}
});
OtherActivity中相应获取数据代码
MyApp app = (MyApp) getApplication();
textView3.setText("name:"+app.getName()
+" age:"+app.getAge()
+" addr:"+app.getAddr());
Application实现类MyApp
package com.xp.android.demo;
import android.app.Application;
public class MyApp extends Application {
String name;
int age;
String addr;
public void onCreate() {
super.onCreate();
name = "tomcat";
age = 12;
addr = "上海";
}
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 String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
在AndroidMainfest.xml清单文件中的application标记加上android:name="com.xp.android.demo.MyApp"
第三种:采用剪切板
MainActivity中按键点击事件
<pre name="code" class="java">button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ClipboardManager manager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String name = "joker";
<span style="white-space:pre"> </span>//传递普通类型数据
<span style="white-space:pre"> </span>ClipData clip = ClipData.newPlainText("name", name);
<span style="white-space:pre"> </span>/*传递对象类型数据
Person person = new Person("rose", 11, "上海");
try {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
ObjectOutputStream obj = new ObjectOutputStream(bao);
obj.writeObject(person);
clip = ClipData.newPlainText("person", Base64.encodeToString(bao.toByteArray(), Base64.DEFAULT));
} catch (IOException e) {
e.printStackTrace();
}
<span style="white-space:pre"> </span>*/
manager.setPrimaryClip(clip);
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
});
OtherActivity中获取数据部分代码
<pre name="code" class="java"> ClipboardManager manager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData data = manager.getPrimaryClip();
String str = data.getItemAt(0).getText().toString();
<span style="white-space:pre"> </span>/*获取对象类型数据
Person person;
try {
ByteArrayInputStream bao = new ByteArrayInputStream(Base64.decode(str, Base64.DEFAULT));
ObjectInputStream obj = new ObjectInputStream(bao);
person = (Person) obj.readObject();
str = person.toString();
obj.close();
} catch (Exception e) {
e.printStackTrace();
}
<span style="white-space:pre"> </span>*/
textView.setText(str);
第四种:采用静态变量
MainActivity中按键点击事件
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
<span style="white-space:pre"> </span>Tool.name = "joker";
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
});
工具类中定义静态变量
<pre name="code" class="java">class Tool{
String name;
}
OtherActivity中获取数据部分代码
textView.setText(Tool.name);