Android学习笔记(二)Activiy学习——界面传值

本文介绍在Android开发中,如何实现在不同Activity间传递数据的方法,包括使用Intent、Bundle及自定义类等进行数据传递,并提供了详细的代码示例。

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

Activity之间传值方法

先创建A.B两个Activiy,在A与B中分别创建一个按钮button,同时分别创建文本框TextView


Intent传值

在A activity中创建按钮监听

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        //创建一个Intent,能够打开B Activiy
        Intent i = new Intent(MainActivity.this,Main2Activity.class);
        //传入数据,参数一传数据名 参数二传数据
        i.putExtra("tag","hello world");
        startActivity(i);
    }
});
在B activity获取到数据

private TextView txView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    Intent i = getIntent();

    txView = (TextView) findViewById(R.id.TxView);
    txView.setText(i.getStringExtra("data"));
}


Bundle数据包传值

在A activity中创建按钮监听

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent i = new Intent(MainActivity.this,Main2Activity.class);

        Bundle b = new Bundle();
        b.putInt("int",123);
        b.putString("string","hello world");
        //可以直接添加到B
        i.putExtras(b);
        //也可以传递Bundle
        i.putExtra("bundle",b);
        startActivity(i);

    }
});

在B activity获取到数据

Intent i = getIntent();
Bundle data = i.getExtras();
txView = (TextView) findViewById(R.id.TxView);
//若没有值,可以设置默认值
txView.setText( String.format("int = %d string = %s name = %s",data.getInt("int"),data.getString("string"),data.getString("name","default")));

//通过key查找bundle
Bundle data1 = i.getBundleExtra("bundle");


传递自定义类

//传递类时,两种方法,Serializable和Parcelable
    //Serializable直接在初始化时传递就可以(速度慢,使用简单)
    //Parcelable需要实现系统的两个方法(速度快,比较支持,使用复杂)

创建class类User

public class User implements Serializable{

    private String name;
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User(String name,int age){
        this.name = name;
        this.age = age;
    }
}

在A activity中

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent i = new Intent(MainActivity.this,Main2Activity.class);

        i.putExtra("user",new User("123",3));
        startActivity(i);

    }
});

在B activity中获取

User user = (User)i.getSerializableExtra("123");
txView.setText(String.format("%s %d",user.getName(),user.getAge()));


使用Parcelable传递类时

public class User implements Parcelable{

    private String name;
    private int age;

    protected User(Parcel in) {
        name = in.readString();
        age = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //保存数据
        dest.writeString(name);
        dest.writeInt(age);
    }

    @Override
    public int describeContents() {
        return 0;
    }
    //创建Creator
    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            //in.readBundle()使用bundle可以写任意数据
            //修改返回值
            return new User(in.readString(),in.readInt());
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //构造方法
    public User(String name,int age){
        this.name = name;
        this.age = age;
    }
}

获取来自B activity的返回值

在button监听里面添加

Intent i = new Intent(MainActivity.this,Main2Activity.class);

startActivityForResult(i,0);

重写方法

//resultCode为result的返回码,data为Intent返回的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    data.getStringExtra("data");
}




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值