安卓开发中Bundle使用方法:
最近工作中涉及很多跟合作伙伴做SDK集成的事情,因为我本身负责产品管理和市场沟通,只懂看一些商务层面和更大框架的东西;之前遇到这种集成的事情都是交给开发的同学去处理就好了,最近发现开发的同学干活有些漂,干点小活就可以对产品经理的我各种JJYY, 惹毛了我! 但是没辙,Team里唯一的技术的同学说的话,也只好就着他;无奈尽管看到客户和市场有需求,产品需要完善,但是受制于人没法推进。毕业后把编程早早甩到一边,现在越来越发现掌握Android这个开发工具的重要性,太多的项目跟他有关,而且Android可以是基于Java开发,Java也是在各个行业应用非常广泛的语音,所以决定狠下心来,逼自己一把,学习一下Android编程. CSDN是个好网站,今天开始在CSDN记录个人的学习的历程,梳理部分遇到的问题:
Bundle 使用方法:
Bundle 直译“捆绑”,在市场营销中常常是把不同的产品或者捆绑一起进行销售的一种策略,通常公司会对这些捆绑的产品和服务组合提供一个总体的折扣价格,让客户购买组合产品时享受比单独购买每件产品更低的价格,以吸消费者买更多产品,从而让公司获得更多的利润;
那用在Android开发里,Bundle这个词个人理解也是起的相当好;Bundle的作用就是从一个Activity里把多个不同的参数(以key-value键值对形式存在) 的组合捆绑起来打包传递给另一个Activity.
Bundle方式:
在发送Activity里面发送key-value对信息:
Intent intent= new Intent(); //首先新建立一个intent intent.setClass(getApplicationContext(),Target.class); //设置intent跳转的目标Activity; Bundle mBundle = new Bundle(); //建立一个mBundle用于存放一个或多个key-value对 mBundle.putString("stringKey1","1.传递String类型数据"); //存放第一个key-value对; mBundle.putString("stringKey2","2.传递另一个String数据类型"); intent.putExtra("bundleName",mBundle);//将mBundle直接放到Extra中,发送到目标Activity; startActivity(intent); //执行intent跳转;
在目标Activity里面接收mBundle传递过来的value:
receiverText = findViewById(R.id.ReceiverText); //这里在目标页设置了一个TextView窗口,非必需,如果数据不用直接在TextView 上呈现就不用设置TextView. 这里只是为了方便查看数据传递效果; Intent intent = getIntent(); //新建一个intent,用于接受发送Activity传送过来的intent; Bundle bundleReceiver = intent.getBundleExtra("bundleName");//新建个bundle接受mBundle; String data1 = bundleReceiver.getString("stringKey1");//讲mBundle中key值为stringKey1的数据接收过来用一个data1盛装; receiverText.setText(data1);//在TextView中打印出data1的数据;
Bundle在发送Activity里面发送key-value对信息时另一种用法:
Intent intent= new Intent(); intent.setClass(getApplicationContext(),Target.class); Bundle mBundle = new Bundle(); mBundle.putString("stringKey1","1.传递String类型数据"); mBundle.putString("stringKey2","2.传递另一个String数据类型"); intent.putExtras(mBundle); //这里直接用intent.putExtras()把mBundle传递;(注意Extras的s不要丢!!) startActivity(intent);
此时,接收Activity也需要相应调整:
receiverText = findViewById(R.id.ReceiverText); Intent intent = getIntent(); Bundle bundleReceiver = intent.getExtras(); String data1 = bundleReceiver.getString("stringKey1"); receiverText.setText(data1);
效果:
全部源码:
manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"package="com.example.testbundle3"><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.TestBundle3"tools:targetApi="31"><activityandroid:name=".TestBundle"android:exported="true"android:label="@string/testBundleActivitylabel"><intent-filter><action android:name="android.intent.action.MAIN"></action><category android:name="android.intent.category.LAUNCHER"></category></intent-filter></activity><activityandroid:name=".Target"android:exported="true"android:label="@string/theTargetActivityLabel"/></application></manifest>
开始Activity对应的类 TestBundle.java
package com.example.testbundle3;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;public class TestBundle extends AppCompatActivity {private Button btton1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btton1 = findViewById(R.id.test);btton1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent= new Intent();intent.setClass(getApplicationContext(),Target.class);Bundle mBundle = new Bundle();mBundle.putString("stringKey1","1.传递String类型数据");mBundle.putString("stringKey2","2.传递另一个String数据类型");intent.putExtras(mBundle);startActivity(intent);}});} }
目标Activity对应的类 target.java
package com.example.testbundle3;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView;import java.lang.reflect.Array; import java.util.ArrayList;public class Target extends AppCompatActivity {private TextView receiverText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.target);receiverText = findViewById(R.id.ReceiverText);Intent intent = getIntent();Bundle bundleReceiver = intent.getBundleExtra("bundleName");String data1 = bundleReceiver.getString("stringKey1");receiverText.setText(data1);} }
开始Activity的页面:main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:orientation="vertical"android:layout_height="match_parent"android:gravity="center"android:paddingLeft="50dp"android:paddingRight="50dp"tools:context=".TestBundle"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="本页是准备传递参数的页面,这里当用户点击“传递参数”按钮时会将多个键值对传递给目标Activity!"android:textSize="20dp"android:textColor="@color/purple_700"></TextView><Buttonandroid:id="@+id/test"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="50dp"android:text="点击跳转"></Button></LinearLayout>
目标Activity的页面:target.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:paddingLeft="30dp"android:paddingRight="30dp"tools:context=".Target"><TextViewandroid:id="@+id/ReceiverText"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/black"android:layout_marginLeft="50dp"></TextView></androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
<resources><string name="app_name">Good Bundle Function Test</string><string name="testBundleActivitylabel">This the start page</string><string name="theTargetActivityLabel">This is the target page</string> </resources>
时间会记录一切~
安卓开发中Bundle使用方法:相关推荐
- 豁然开朗篇:安卓开发中关于线程那些事(下篇)
彻底搞懂线程这一块,看这一篇就够了 前言 本系列详细讲解并发的知识,从基础到底层,让大家彻底搞懂线程和锁的原理,当然里面会涉及到一些内存结构的知识,所以如果为了更好地阅读效果,也可以先去看以下这两篇: ...
- 豁然开朗篇:安卓开发中关于虚拟机那些事
彻底搞懂虚拟机这一块,看这一篇就够了 前言 作为豁然开朗篇的最终篇,本文要讲解的是虚拟机这块,因为在之前讲解内存与线程的时候,一直都会牵涉到虚拟机和指令集这块,所以,为了让大家再豁然开朗多一次,本文会 ...
- 安卓开发中,release安装包安装后,打开app后再按home键,再次点击程序图标app再次重新启动的解决办法
安卓开发中,release安装包安装后,打开app后再按home键,再次点击程序图标app再次重新启动的解决办法 在开发中我们一般都是直接AS上的安装(Run)按钮,直接安装到真机或模拟器上进行测试, ...
- Android安卓开发中图片缩放讲解
安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法: 方法1:按固定比例进行缩放 在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们 ...
- Android Studio安卓开发中使用json来作为网络数据传输格式
如果你是在安卓开发中并且使用android studio,要使用json来作为数据传输的格式,那么下面是我的一些经验. 一开始我在android studio中导入那6个包,那6个包找了非常久,因为放 ...
- 安卓开发中的USB转串口通讯
安卓开发中的USB转串口通讯 本文使用GitHub上开源的"hoho.android.usbserial"USB串口库.该库基于"Android USB Host API ...
- 安卓开发中的重力感应传感器
2019独角兽企业重金招聘Python工程师标准>>> 安卓开发中拥有多种传感器,google提供了11种传感器供应用层使用:加速度.磁力.方向.陀螺仪.光线.压力(返回当前压强). ...
- 安卓开发中ToggleButton按钮的使用
ToggleButton(开关按钮)是Android系统中比较简单的一个组件,是一个具有选中和未选择状态双状态的按钮,并且需要为不同的状态设置不同的显示文本.下面以一个简单的控制室内设备的开关为例来介 ...
- 安卓开发中非常炫的效果集合
安卓开发中非常炫的效果集合 这几天开发的时候,想做一些好看而且酷炫的特效,于是又开始从网上收集各种特效资源.下面给大家一些我喜欢的把,附代码,喜欢的看源代码,然后加到自己项目去把!! 一个开源项目网站 ...
最新文章
- Django rest_framework 认证源码流程
- 数组的定义格式一_动态初始化
- mysql innodb 索引 延迟更新_Mysql覆盖索引与延迟关联
- 开发人员生产力指南,细节决定成败!
- Facebook要造芯片了,开发团队正在组建中
- IIS 7 启用 gzip 静态压缩 压缩js和css文件
- RabbitMQ项目实战——商户管理系统
- BI报表工具FineReport的使用2【帆软聚合报表设计,最常用功能】
- 轻量级服务器和ECS云服务器有什么区别?
- Cocos2d-JS 项目接入AnySDK
- 日本企业遭遇严重用工短缺
- reporting services报表部署错误:运行配置文件中指定的扩展时出现异常。 ---> 超过了最大请求长度。
- ubuntu18.04 升级内核后,进入系统页面卡在“started gnome display manager“的解决方案
- 质因数分解,(结果简化版)
- 微信公众号实现“一键关注”功能
- SQL入门-连结(JOIN)
- RabbitMQ消息队列,发送消息失败、消息持久化、消费者失败处理方法和发送消息
- 如何围绕某一轴(不是xyz)旋转——transform.RotateAround
- Hero项目分析(基于Angular架构)
- 在CentOS 7中安装Panoply