当前位置: 首页 > news >正文

【Android开发】不同Activity之间的数据回传实例(一)摘桃子游戏

一、功能介绍

该项目实现的功能主要有:

  1. 在首页显示一个按钮点击该按钮跳转到桃园页面
  2. 在桃园页面,点击桃子会弹窗显示摘到几个桃子,同时被点击桃子消失,总桃子数+1
  3. 点击退出桃园会返回首页,首页桃子数会根据点击的桃子数动态增加

二、代码实现

1. 资源准备

将项目所需要的图片bg.png、monkey.png、btn_peach.png、peach_pic.png 导入程序的drawablehdpi文件夹中(默认情况下程序中没有drawable-hdpi 文件夹,需手动在res 文件夹中创建一个)。

2. 布局文件设计

2.1 主Activity

在layout文件夹中编辑activity_main.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"><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:background="#008577"android:gravity="center"android:text="首页"android:textColor="@android:color/white"android:textSize="20sp" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg"android:gravity="center_vertical"><ImageViewandroid:id="@+id/iv_monkey"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/monkey" /><Buttonandroid:id="@+id/btn_peach"android:layout_width="80dp"android:layout_height="40dp"android:layout_marginLeft="30dp"android:layout_marginTop="250dp"android:layout_toRightOf="@id/iv_monkey"android:background="@drawable/btn_peach"android:gravity="center"android:text="去桃园"android:textColor="@android:color/black"android:textSize="18sp" /><ImageViewandroid:id="@+id/iv_peach"android:layout_width="45dp"android:layout_height="35dp"android:layout_below="@+id/btn_peach"android:layout_centerHorizontal="true"android:layout_marginTop="20dp"android:src="@drawable/peach_pic" /><TextViewandroid:id="@+id/tv_count"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/btn_peach"android:layout_marginTop="25dp"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/iv_peach"android:text="摘到0 个"android:textColor="@android:color/black"android:textSize="20sp" /></RelativeLayout>
</LinearLayout>
2.2 辅Activity

在layout文件夹中新增activity_peach.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"><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:background="#008577"android:gravity="center"android:text="桃园"android:textColor="@android:color/white"android:textSize="20sp" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/tree_bg"><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_marginTop="70dp"android:background="@drawable/tree"><Buttonandroid:id="@+id/btn_one"android:layout_width="55dp"android:layout_height="40dp"android:layout_marginLeft="140dp"android:layout_marginTop="35dp"android:background="@drawable/peach_pic" /><Buttonandroid:id="@+id/btn_two"android:layout_width="55dp"android:layout_height="40dp"android:layout_below="@id/btn_one"android:layout_marginLeft="80dp"android:layout_marginTop="5dp"android:background="@drawable/peach_pic" /><Buttonandroid:id="@+id/btn_three"android:layout_width="55dp"android:layout_height="40dp"android:layout_below="@id/btn_one"android:layout_marginLeft="70dp"android:layout_marginTop="5dp"android:layout_toRightOf="@id/btn_two"android:background="@drawable/peach_pic" /><Buttonandroid:id="@+id/btn_four"android:layout_width="55dp"android:layout_height="40dp"android:layout_below="@id/btn_two"android:layout_marginLeft="25dp"android:layout_marginTop="15dp"android:background="@drawable/peach_pic" /><Buttonandroid:id="@+id/btn_five"android:layout_width="55dp"android:layout_height="40dp"android:layout_below="@id/btn_two"android:layout_marginLeft="70dp"android:layout_marginTop="15dp"android:layout_toRightOf="@id/btn_four"android:background="@drawable/peach_pic" /><Buttonandroid:id="@+id/btn_six"android:layout_width="55dp"android:layout_height="40dp"android:layout_below="@id/btn_two"android:layout_marginLeft="45dp"android:layout_marginTop="15dp"android:layout_toRightOf="@id/btn_five"android:background="@drawable/peach_pic" /></RelativeLayout><Buttonandroid:id="@+id/btn_exit"android:layout_width="100dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:layout_alignParentBottom="true"android:layout_margin="50dp"android:background="@drawable/btn_peach"android:text="退出桃园"android:gravity="center"android:textColor="@android:color/black"android:textSize="18sp"/></RelativeLayout>
</LinearLayout>
2.3 其他资源文件

主题文件themes.xml

<resources xmlns:tools="http://schemas.android.com/tools"><!-- Base application theme. --><style name="Theme.PickPeach" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge"><!-- Primary brand color. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryVariant">@color/colorPrimaryDark</item><item name="colorOnPrimary">@color/white</item><!-- Secondary brand color. --><item name="colorSecondary">@color/teal_200</item><item name="colorSecondaryVariant">@color/teal_700</item><item name="colorOnSecondary">@color/black</item><!-- Status bar color. --><item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item><!-- Customize your theme here. --></style><style name="MyCustomTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- Primary brand color. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryVariant">@color/colorPrimaryDark</item><item name="colorOnPrimary">@color/white</item><!-- Secondary brand color. --><item name="colorSecondary">@color/teal_200</item><item name="colorSecondaryVariant">@color/teal_700</item><item name="colorOnSecondary">@color/black</item><!-- Status bar color. --><item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item><!-- Customize your theme here. --></style>
</resources>

3. 实现逻辑功能

3.0 全局配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.hzj.pickpeach"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/MyCustomTheme"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".PeachActivity"android:exported="true"></activity></application></manifest>
3.1 首页功能

在MainActivity 中实现“去桃园”按钮的点击事件,当点击按钮时程序跳转到桃园摘桃的界面。同时,还需要接收桃园界面回传过来的桃子个数。

public class MainActivity extends AppCompatActivity {private Button btn_peach;private TextView tv_count;private int totalCount = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();}private void init() {btn_peach = findViewById(R.id.btn_peach);tv_count = findViewById(R.id.tv_count);//为按钮设置点击事件监听器btn_peach.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//设置意图对象,实现界面跳转Intent intent = new Intent(MainActivity.this, PeachActivity.class);startActivityForResult(intent, 1);
//                startActivity(intent);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);//判断请求码和返回码是否正确if (requestCode==1 && requestCode==1) {//获取回传的数据int count = data.getIntExtra("count", 0);;totalCount = totalCount + count;tv_count.setText("摘到" + totalCount + "个");}}
}
3.2 桃园界面的摘桃效果

点击桃子实现摘桃效果,同时全局桃子数动态变化,点击退出桃园,返回首页,同时更新首页桃子数。

public class PeachActivity extends AppCompatActivity implements View.OnClickListener {private Button btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_exit;private int count = 0;//桃子个数@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_peach);init();}private void init() {btn_one = findViewById(R.id.btn_one);btn_two = findViewById(R.id.btn_two);btn_three = findViewById(R.id.btn_three);btn_four = findViewById(R.id.btn_four);btn_five = findViewById(R.id.btn_five);btn_six = findViewById(R.id.btn_six);btn_exit = findViewById(R.id.btn_exit);btn_one.setOnClickListener(this);btn_two.setOnClickListener(this);btn_three.setOnClickListener(this);btn_four.setOnClickListener(this);btn_five.setOnClickListener(this);btn_six.setOnClickListener(this);btn_exit.setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.btn_one: //第一个桃子的点击事件info(btn_one);break;case R.id.btn_two: //第二个桃子的点击事件info(btn_two);break;case R.id.btn_three: //第三个桃子的点击事件info(btn_three);break;case R.id.btn_four: //第四个桃子的点击事件info(btn_four);break;case R.id.btn_five: //第五个桃子的点击事件info(btn_five);break;case R.id.btn_six: //第六个桃子的点击事件info(btn_six);break;case R.id.btn_exit: //“退出桃园”按钮的点击事件returnData();break;default:throw new IllegalStateException("Unexpected value: " + view.getId());}}/*** 按钮的点击事件处理*/private void info(Button btn) {//桃子个数加1count++;//被摘掉的桃子设置为隐藏不可见btn.setVisibility(View.INVISIBLE);Toast.makeText(PeachActivity.this, "摘到" + count + "个桃子",Toast.LENGTH_LONG).show();}/*** 将数据回传到上个界面*/private void returnData() {//设置意图对象,将摘桃子的个数回传给首页界面Intent intent = new Intent();intent.putExtra("count", count);//返回码标识setResult(1, intent);//销毁本界面PeachActivity.this.finish();}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {//当点击设备返回键时,调用数据回传方法,返回首页界面if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {returnData();}return false;}
}

三、效果展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

相关文章:

【Android开发】不同Activity之间的数据回传实例(一)摘桃子游戏

一、功能介绍 该项目实现的功能主要有&#xff1a; 在首页显示一个按钮点击该按钮跳转到桃园页面在桃园页面&#xff0c;点击桃子会弹窗显示摘到几个桃子&#xff0c;同时被点击桃子消失&#xff0c;总桃子数1点击退出桃园会返回首页&#xff0c;首页桃子数会根据点击的桃子数…...

增量式PID和脉冲轴组合控制阀门开度(算法介绍)

这篇博客我们以S7-1200PLC平台来举例,介绍我们的PID闭环控制器如何控制脉冲轴实现阀门角度控制。SMART PLC PID控制器控制伺服驱动器实现关节角度控制详细内容请参考下面文章: https://rxxw-control.blog.csdn.net/article/details/129658364https://rxxw-control.blog.csdn…...

解决Vue.js Devtools未检测到Vue实例的问题

解决Vue.js Devtools未检测到Vue实例的问题 解决Vue.js Devtools未检测到Vue实例的问题1. 确保Vue.js已正确加载1.1 在HTML文件中直接引入1.2 在构建工具&#xff08;如Webpack&#xff09;中配置引入1.3 检查与验证 2. 检查Vue.js Devtools扩展安装状态2.1 打开Chrome浏览器扩…...

【Java基础】进程与线程,并发与并行,CPU单核与多核

目录 1 进程与线程2 CPU单核与多核 1 进程与线程 进程与线程基本单位的对象不同 进程是操作系统进行资源分配&#xff08;包括cpu、内存、磁盘IO等&#xff09;的最小单位线程是CPU调度和分配的基本单位 CPU看不到进程&#xff0c;只能看到待分配的一些线程 并发与并行 并发&…...

git修改最新提交(commit)信息

一、修改最近一次commit信息 1、首先通过git log查看commit信息 2、使用命令git commit --amend进入命令命令模式&#xff0c;按i进入编辑模式&#xff0c;修改好commit信息后按Esc键退出编辑模式&#xff0c;然后输入:wq保存编辑信息&#xff08;注意使用英文输入法&#xf…...

想寻找Axure的替代品?我们已经试用了10+款设计工具,来看看吧!

Axure是许多产品经理和设计师进入快速原型设计的首选工具&#xff0c;但Axure的使用成本相对较高&#xff0c;学习曲线陡峭&#xff0c;许多设计师正在寻找可以取代Axure的原型设计工具&#xff0c;虽然现在有很多可选的设计工具&#xff0c;但质量不均匀&#xff0c;可以取代A…...

报文大小限制、请求体类型总结

文章目录 1. 各节点请求体有无限制1.1 http协议1.2 TCP/IP层限制1.3 浏览器1.4 nginx1.5 gateway1.6 tomcat1.7 springboot1.8 内存、磁盘处理不了一切白搭 2. 请求体类型2.1 application/x-www-form-urlencoded2.2 multipart/form-data2.3 application/json2.4 text/plain2.5 …...

rknn加载onnx时报错 GLIBC=2.29 no found librknnc.so

rknn 中onnx转rknn在虚拟机中运行时发现报错. GLIBC2.29 no found /****/librknnc.so 昨天还正常的, 今天装了个ftp 和宝塔面板就出错了. 我估计根据报错地址, 找到了librknnc.so文件, 权限也给了777仍然不行 , 我怀疑是GLIBC的版本不对 ,网上给的方法是下载源码, 然后自己手动…...

ASP .net core微服务实战(杨中科)

背景&#xff1a; 主要是思考下&#xff0c;我们为什么要用微服务&#xff1f; 微服务我现在理解是&#xff1a;提供了我们一种模块化的手段&#xff0c;一个服务负责一种类型的业务&#xff0c;是一种面对复杂问题进行拆分的方式&#xff0c;但是也会引入一些中间件&#xf…...

使用命令行方式搭建uni-app + Vue3 + Typescript + Pinia + Vite + Tailwind CSS + uv-ui开发脚手架

使用命令行方式搭建uni-app Vue3 Typescript Pinia Vite Tailwind CSS uv-ui开发脚手架 项目代码以上传至码云&#xff0c;项目地址&#xff1a;https://gitee.com/breezefaith/uniapp-vue3-ts-scaffold 文章目录 使用命令行方式搭建uni-app Vue3 Typescript Pinia V…...

VUE+bpmn.js实现工作流

1、安装bpmn.js npm install bpmn-js7.3.1 // 我安装的版本是7.3.1npm install bpmn-js-properties-panel0.37.2npm install bpmn-moddle7.1.3 npm install --save camunda-bpmn-moddle 2、配置axios&#xff0c;在main.js中引入axios import axios from axiosVue.proto…...

微信小程序Burp抓包

方法有很多&#xff0c;工具也各有差异&#xff0c;主要是学代理流量的思路 Burp流量代理工具小程序 一、Burp证书导入 1、开启代理 开启浏览器的代理&#xff0c;火狐推荐FoxyProxy&#xff0c;Google推荐SwitchyOmega&#xff0c;设置代理为127.0.0.1:8080。 2、下载证书…...

基础篇_面向对象(什么是对象,对象演化,继承,多态,封装,接口,Service,核心类库,异常处理)

文章目录 一. 什么是对象1. 抽取属性2. 字段默认值3. this4. 无参构造5. 抽取行为 二. 对象演化1. 对象字段演化2. 对象方法演化3. 贷款计算器 - 对象改造4. 静态变量5. 四种变量 三. 继承1. 继承语法2. 贷款计算器 - 继承改造3. java 类型系统4. 类型转换1) 基本类型转换2) 包…...

【一、测试基础】Java基础语法

Java 的用法及注意事项有很多&#xff0c;今天的目标是了解Java基础语法&#xff0c;且能够输出"hello world" 几个基础的概念 对象&#xff1a;对象是类的一个实例&#xff0c;有状态和行为。一只猫是一个对象&#xff0c;猫的状态有&#xff1a;颜色、名字、品种&…...

社交距离 - 华为OD统一考试

OD统一考试(C卷) 分值: 200分 题解: Java / Python / C++ 题目描述 疫情期间,需要大家保证一定的社交距离,公司组织开交流会议,座位有一排共N个座位,编号分别为[0…N-1],要求员工一个接着一个进入会议室,并且可以在任何时候离开会议室。 满足:每当一个员工进入时,…...

Odrive 学习系列一:vscode 编译Odrive

搭建环境可参考Markerbase教程,很详细了。 简单说一两点: 解压ODrive-fw-v0.5.1.zip: 打开ODrive-fw-v0.5.1文件夹,找到Firmware文件夹,用vscode打开该文件夹: 按照以下内容操作: 编译工程: 打开 中断(terminal),输入 make -j4 回车 进行编译。编译…...

Pandas实战100例 | 案例 21: 条件运算

案例 21: 条件运算 知识点讲解 在 Pandas 中进行条件运算可以用于创建新的列或修改现有的列&#xff0c;基于一定的条件逻辑。这些运算通常结合布尔索引或 apply 方法进行。 布尔条件运算: 可以根据列之间的比较生成布尔值列。apply 方法进行条件运算: 使用 apply 方法可以在…...

Unity组件开发--长连接webSocket

1.下载安装UnityWebSocket 插件 https://gitee.com/cambright/UnityWebSocket/ 引入unity项目&#xff1a; 2.定义消息体结构&#xff1a;ExternalMessage和包结构Package&#xff1a; using ProtoBuf; using System; using System.Collections; using System.Collections.Ge…...

书客、柏曼、松下护眼台灯哪款更靠谱?实测核心数据对比PK!

随着科技时代的到来&#xff0c;人们的生活水平在不断提高&#xff0c;不少家长开始担心自家孩子的近视问题&#xff0c;护眼台灯在家庭中的讨论热度也越来越高&#xff0c;光线舒适又具备多种功能&#xff0c;不少家长都给孩子入手了护眼台灯。不过作为家电博主&#xff0c;我…...

MQTT协议

一.MQTT协议概述 MQTT&#xff08;Message Queuing Telemetry Transport&#xff09;是一种轻量级的、基于发布/订阅模式的消息传输协议&#xff0c;广泛应用于物联网&#xff08;IoT&#xff09;领域中的设备连接、传感器数据传输等场景。 MQTT协议使用TCP/IP协议栈作为底层…...

费雪的竞争优势分析框架

费雪的竞争优势分析框架 关键词:费雪竞争优势分析框架、企业竞争优势、财务分析、行业分析、企业战略 摘要:本文深入探讨了费雪的竞争优势分析框架。该框架是评估企业竞争力的重要工具,通过多维度的分析帮助投资者和企业管理者判断企业在市场中的地位和发展潜力。文章首先介…...

从‘梯度裁剪’到‘权重初始化’:一份预防梯度爆炸的PyTorch/TensorFlow实操清单

从‘梯度裁剪’到‘权重初始化’&#xff1a;一份预防梯度爆炸的PyTorch/TensorFlow实操清单 训练深度神经网络时&#xff0c;梯度爆炸问题就像一颗定时炸弹——它可能在你最意想不到的时候突然引爆&#xff0c;导致损失函数值瞬间变为NaN&#xff0c;或者权重更新出现剧烈震荡…...

vLLM-v0.17.1GPU优化:显存碎片率<5%的PagedAttention内存管理实录

vLLM-v0.17.1 GPU优化&#xff1a;显存碎片率<5%的PagedAttention内存管理实录 1. vLLM框架简介 vLLM是一个专注于大语言模型(LLM)推理和服务的高性能开源库。这个项目最初由加州大学伯克利分校的天空计算实验室开发&#xff0c;现在已经发展成为一个由学术界和工业界共同…...

别只刷题了!用Python/C++搞定考研机试高频算法(附PIPIOJ真题代码重构与优化)

从暴力解法到优雅实现&#xff1a;Python/C双语言拆解考研机试高频算法 考研机试不仅考察算法理解&#xff0c;更检验工程化编码能力。许多考生能写出正确但冗长的代码&#xff0c;却在时间优化和代码简洁性上失分。本文将用Python和C对比实现六大高频题型&#xff0c;重点分析…...

Maestro Studio终极指南:零代码可视化移动应用测试,5分钟上手自动化

Maestro Studio终极指南&#xff1a;零代码可视化移动应用测试&#xff0c;5分钟上手自动化 【免费下载链接】maestro Painless E2E Automation for Mobile and Web 项目地址: https://gitcode.com/GitHub_Trending/ma/maestro 还在为复杂的移动应用测试流程而烦恼吗&am…...

数据库智能运维:利用PyTorch LSTM预测数据库性能瓶颈

数据库智能运维&#xff1a;利用PyTorch LSTM预测数据库性能瓶颈 1. 引言&#xff1a;当数据库遇上AI预测 凌晨三点&#xff0c;运维工程师小李被刺耳的报警声惊醒——核心数据库又崩溃了。这已经是本月第三次因为性能瓶颈导致的业务中断&#xff0c;每次损失都超过百万。传统…...

3D Face HRN效果验证:使用MeshLab量化评估3D重建PSNR与SSIM指标

3D Face HRN效果验证&#xff1a;使用MeshLab量化评估3D重建PSNR与SSIM指标 1. 项目背景与验证意义 3D人脸重建技术近年来取得了显著进展&#xff0c;但如何客观评估重建质量一直是个关键问题。传统的主观视觉评估方法存在明显局限性——不同观察者可能有不同的判断标准&…...

小程序对商家私域运营到底有多重要?

小程序对商家私域运营到底有多重要&#xff1f;在企业持续获取客户成本不断上升的背景下&#xff0c;越来越多商家开始关注“私域运营”&#xff0c;而小程序也逐渐成为这一体系中的核心工具。小程序对商家私域运营的重要性&#xff0c;本质上体现在“用户沉淀能力与转化效率的…...

2026春SDU软件创新实训第四周个人工作总结

第四周我做的工作总结如下&#xff1a; 从原始网页爬取科一科四题库 书接上期&#xff0c;我们找到了两个网页的题库&#xff08;驾照吧&#xff08;https://www.jiazhaoba.com/tiba&#xff09;元贝驾考&#xff08;https://www.ybjk.com/tiku&#xff09;&#xff09;&…...

告别手动点鼠标!用Python脚本批量跑Simulink仿真,效率提升10倍

告别手动点鼠标&#xff01;用Python脚本批量跑Simulink仿真&#xff0c;效率提升10倍 在工程仿真领域&#xff0c;Simulink无疑是建模与分析的利器。但当面对参数扫描、蒙特卡洛分析或设计迭代等需要大量重复仿真的场景时&#xff0c;手动操作不仅效率低下&#xff0c;还容易…...