之前我写了一篇介绍 Android Activity 的博客,一个应用通常包含多个Activity,在多个Activity 之间传递数据该怎么实现呢?今天就来讨论这个问题。
首先我们建立一个start activity
package com.justin.example;
import com.example.androidexample.R;
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;
import android.widget.EditText;
import android.widget.RadioButton;
public class BundleStartActivity extends Activity {
private Button btn;
private EditText name;
private EditText password;
private RadioButton male;
private String gender;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle_start);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
name = (EditText) findViewById(R.id.name);
password = (EditText) findViewById(R.id.passwd);
male = (RadioButton) findViewById(R.id.male);
gender = male.isChecked()?"男":"女";
Person p = new Person(name.getText().toString(),password.getText().toString(),gender);
Bundle data = new Bundle();
data.putSerializable("person", p);
Intent intent = new Intent(BundleStartActivity.this,BundleResultActivity.class);
intent.putExtras(data);
startActivity(intent);
}
});
}
}
start activity 的界面布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="输入<span style="font-family:Microsoft YaHei;">注册信</span>息"
android:textSize="20sp"/>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="16sp"/>
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写想注册的账号"
android:selectAllOnFocus="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="16sp"/>
<EditText
android:id="@+id/passwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password = "true"
android:selectAllOnFocus="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="性别"
android:textSize="16sp"
/>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textSize="16sp"/>
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textSize="16sp"/>
</RadioGroup>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='注册'
android:textSize="16sp"/>
</TableRow>
</TableLayout>
</LinearLayout>
Result Activity
package com.justin.example;
import com.example.androidexample.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class BundleResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle_result);
TextView name = (TextView)findViewById(R.id.name);
TextView passwd = (TextView)findViewById(R.id.passwd);
TextView gender = (TextView)findViewById(R.id.gender);
Intent intent = getIntent();
Bundle data = intent.getExtras();
Person p = (Person)data.getSerializable("person");
name.setText(p.getName());
passwd.setText(p.getPass());
gender.setText(p.getGender());
}
}
还有一个是Person类
package com.justin.example;
import java.io.Serializable;
public class Person implements Serializable {
private String _Name;
private String _Passwd;
private String _Gender;
public String getName()
{
return _Name;
}
public String getPass()
{
return _Passwd;
}
public String getGender()
{
return _Gender;
}
public Person(String Name,String Passwd,String Gender)
{
this._Name = Name;
this._Passwd = Passwd;
this._Gender = Gender;
}
}
result activity 的界面布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="您的注册信息"
android:textSize="20sp"/>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="16sp"/>
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="16sp"/>
<EditText
android:id="@+id/passwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="16sp"/>
<TextView
android:id="@+id/gender"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"/>
</TableRow>
</TableLayout>
</LinearLayout>
注意:不要忘记把两个Activity注册到AndroidManifest.xml中
点击注册之后的效果
这样就从第一个activity 传值到第二个 activity 了,注意传递的对象需要实现Serializable类。