Android之旅第三站——活动(Activity)的携带Javabean数据的跳转…

本文介绍了在Android开发中如何使用Javabean进行活动(Activity)之间的数据传递,强调了实现Serializable接口的重要性,并提供了一个用户信息填写的案例,详细解释了从创建Javabean到在不同Activity间传递并恢复数据的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值