Javabean数据的跳转是最常用,也是最常见的一种,有时候并不是只能携带一种类型的数据,需要将一堆不同类型数据一块传到
下一个Activity中,那么,Javabean就很完美的解决了很多问题。
可以想这么一个问题,很多时候在填写个人信息,往往这些都是一个人作为一个对象所具有的。那么就可以定义一个用户类,通过
类的对象来获取相对应的信息。
先来看看效果图:
提交信息后:
要想使用Javabean需要注意几个步骤:
1、javabean 实现 serialrizable接口。
2、putExtra需要选择serialrizable类型的。
3、接收Intent对象后通过调用getserialrizableExtra获得serialrizable对象。
4、将serialrizable对象强制转换成User类的Javabean对象。
下面附上代码:
Mainactivity:
package com.example.jbtz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText et,et1,et2,et3;
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
registerListenners();
}
private void registerListenners() {
// TODO Auto-generated method stub
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name=et.getText().toString().trim();
String sex=et1.getText().toString().trim();
String age=et2.getText().toString().trim();
String add=et3.getText().toString().trim();
User user=new User(name, sex, age, add);
Intent intent = new Intent();
intent.putExtra("user", user);
intent.setClass(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
});
}
private void initViews() {
// TODO Auto-generated method stub
bt=(Button) findViewById(R.id.bt);
et=(EditText) findViewById(R.id.et);
et1=(EditText) findViewById(R.id.et1);
et2=(EditText) findViewById(R.id.et2);
et3=(EditText) findViewById(R.id.et3);
}
}
Otheractivity:
package com.example.jbtz;
import java.io.Serializable;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity {
private TextView tv,tv1,tv2,tv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
initViews();
}
private void initViews() {
// TODO Auto-generated method stub
tv=(TextView) findViewById(R.id.tv);
tv1=(TextView) findViewById(R.id.tv1);
tv2=(TextView) findViewById(R.id.tv2);
tv3=(TextView) findViewById(R.id.tv3);
Intent intent=getIntent();
Serializable s= intent.getSerializableExtra("user");
User u = (User) s;
tv.setText("您的姓名:"+u.getName());
tv1.setText("您的性别:"+u.getSex());
tv2.setText("您的年龄:"+u.getAge());
tv3.setText("您的地址:"+u.getAdd());
}
}
同样需要在androidmanifest中注册OtherActivity才可以用。
User类:
package com.example.jbtz;
import java.io.Serializable;
public class User implements Serializable{
private String name;
private String sex;
private String age;
private String add;
public User(String name, String sex, String age, String add) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.add = add;
}
public User() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
@Override
public String toString() {
return "User [name=" + name + ", sex=" + sex + ", age=" + age + ", add=" + add + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((add == null) ? 0 : add.hashCode());
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (add == null) {
if (other.add != null)
return false;
} else if (!add.equals(other.add))
return false;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
}
第一个Activity的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="请填写个人信息"
android:textSize="30sp"
android:textStyle="bold|italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名: "
android:textSize="20sp" />
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别: "
android:textSize="20sp" />
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄: "
android:textSize="20sp" />
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="地址: "
android:textSize="20sp" />
<EditText
android:id="@+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="提交并显示信息" />
</LinearLayout>
第二个Activity的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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="20sp" />
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:textSize="20sp" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:textSize="20sp" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:textSize="20sp" />
</LinearLayout>