android Activity 页面传值

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");

然后循环遍历




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android页面传值可以通过Intent传递数值或者使用Bundle传递数据。在第一个页面中,可以使用Intent.putExtra()方法将数据放入Intent中,然后通过startActivity()方法启动第二个页面来传递数值。在第二个页面中,可以使用getIntent()方法获取传递过来的Intent,然后使用getIntExtra()或者getStringExtra()方法获取传递的数值。\[1\] 另外一种方法是使用Bundle传递数据。在第一个页面中,可以创建一个Bundle对象,使用bundle.putInt()或者bundle.putString()方法将数据放入Bundle中,然后使用Intent.putExtras()方法将Bundle放入Intent中,最后通过startActivity()方法启动第二个页面来传递数据。在第二个页面中,可以使用getIntent()方法获取传递过来的Intent,然后使用getExtras()方法获取传递的Bundle,再使用bundle.getInt()或者bundle.getString()方法获取传递的数据。\[2\] 总结来说,Android页面传值可以通过Intent传递数值或者使用Bundle传递数据,具体的方法根据需求选择合适的方式进行传值。 #### 引用[.reference_title] - *1* *2* [Android传值](https://blog.csdn.net/wzx311/article/details/118582445)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [安卓的页面之间传值](https://blog.csdn.net/m0_72429728/article/details/127219725)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值