android Activity 页面传值

本文介绍了在Android开发中如何在不同Activity之间传递数据,包括基本数据类型、多条数据、对象及对象集合的传递方法。

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

android Ativity之间传值一般使用Intent这个类



一、单行数据

    传值

         Intent intent=new Intent(this,ThinkActivity.class);
        //传递基本数据类型
        //intent.putExtra("uname",传递的数据);       

        拿值
       String uname=getIntent().getStringExtra("uname");



二、多行数据

       这里我们需要使用Bundle

  传值

 //传递Bundle
         Intent intent=new Intent(this,ThinkActivity.class);
Bundle bundle=new Bundle(); bundle.putString("uname",数据); bundle.putInt("uage",数据); intent.putExtra("bundle",bundle);

拿值

 //接受bundle
        Bundle bundle=getIntent().getBundleExtra("bundle");
        String uname=bundle.getString("uname");
        int uage=bundle.getInt("uage");

   三、传对象(需要将对象进行序例化)

  传值

	Intent intent=new Intent(this,ThinkActivity.class);


 	Person person=new Person(1,"静静",19);
        intent.putExtra("persons",persons);
拿值

	Person person= (Person) getIntent().getSerializableExtra("person");
        Person person=getIntent().getParcelableExtra("person");

     四、传对象集合(需要使用Android的方法将对象进行实例化)

传值

	Intent intent=new Intent(this,ThinkActivity.class); 	

	Person person1=new Person(1,"静静",19);
        Person person2=new Person(2,"明明",19);
        Person person3=new Person(3,"雨田君",19);

        ArrayList<Person> persons=new ArrayList<>();
        persons.add(person1);
        persons.add(person2);
        persons.add(person3);

        //传递对象集合
        intent.putParcelableArrayListExtra("persons",persons);
拿值

List<Person> persons=getIntent().getParcelableArrayListExtra("persons");

然后循环遍历




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值