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

【Android嵌入式开发及实训课程实验】【项目1】 图形界面——计算器项目

【项目1】 图形界面——计算器项目

  • 需求分析
  • 界面设计
  • 实施
    • 1、创建项目
    • 2、 界面实现
    • 实现代码
      • 1.activity_main.xml
      • 2.Java代码 - MainActivity.java
    • 3、运行测试
  • 注意点
  • 结束~

需求分析

开发一个简单的计算器项目,该程序只能进行加减乘除运算。要求界面美观,使用方便。
为降低编程难度,本计算器不支持连计算和混合运算。

界面设计

计算器项目的界面如图,具体内容包括1个为文本显示框,用于显示用户的按键输入值及计算结果;
18个按钮,即0~9数字键,加减乘除、小数点、等于号,以及清除按钮CLEAR和退格按钮BACKSPACE。

在这里插入图片描述

实施

1、创建项目

 创建一个名为 Calculator的项目,为简单起见,在开发过程中只使用默认的布局文件 activity_main.xml 和 MainActivity类。

2、 界面实现

计算器项目的界面实现思想:外层采用垂直线性布局,内层嵌套水平线性布局。本项目中的activity_main.xml 的图形控件及其Text、ID属性如下:

在这里插入图片描述

在本项目中,为所有的按钮指定相同的onClick属性,其事件处理的方法名全部为onClick。

实现代码

1.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:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:id="@+id/tvResult"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Medium Text"android:textAppearance="?android:attr/textAppearanceMedium" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/btnClear"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:onClick="onClick"android:text="Clear" /><Buttonandroid:id="@+id/btnBackSpace"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:onClick="onClick"android:text="Backspace" /></LinearLayout><!-- 第一行 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/btn7"android:layout_width="wrap_content"android:layout_weight="1"android:layout_height="wrap_content"android:onClick="onClick"android:text="7" /><Buttonandroid:layout_weight="1"android:id="@+id/btn8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="8" /><Buttonandroid:layout_weight="1"android:id="@+id/btn9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="9" /><Buttonandroid:layout_weight="1"android:id="@+id/btnDevide"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="/" /></LinearLayout><!--2--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><Buttonandroid:layout_weight="1"android:id="@+id/btn4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="4" /><Buttonandroid:layout_weight="1"android:id="@+id/btn5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="5" /><Buttonandroid:id="@+id/btn6"android:layout_weight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="6" /><Buttonandroid:layout_weight="1"android:id="@+id/btnMultiply"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="*" /></LinearLayout><!--3--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><Buttonandroid:layout_weight="1"android:id="@+id/btn1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="1" /><Buttonandroid:layout_weight="1"android:id="@+id/btn2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="2" /><Buttonandroid:id="@+id/btn3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="onClick"android:text="3" /><Buttonandroid:layout_weight="1"android:id="@+id/btnMinus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="-" /></LinearLayout><!--4--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><Buttonandroid:layout_weight="1"android:id="@+id/btnDot"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="." /><Buttonandroid:layout_weight="1"android:id="@+id/btn0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="0" /><Buttonandroid:layout_weight="1"android:id="@+id/btnEqual"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="=" /><Buttonandroid:layout_weight="1"android:id="@+id/btnPlus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="onClick"android:text="+" /></LinearLayout></LinearLayout>

2.Java代码 - MainActivity.java

Activity类用于实现项目的功能,包括对按钮的响应及计算数值。代码如下
package com.example.administrator.calculator;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;import com.example.ex2mycalculator.R;import java.util.regex.Matcher;
import java.util.regex.Pattern;public class MainActivity extends AppCompatActivity {TextView tvResult;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tvResult = findViewById(R.id.tvResult);tvResult.setText("");}@SuppressLint("NonConstantResourceId")public void onClick(View v){Button b = (Button) v;String btnText = b.getText().toString();String tvText = tvResult.getText().toString();int btnClear = R.id.btnClear;int id = v.getId();if (id == R.id.btnClear) {tvResult.setText("");} else if (id == R.id.btn0 || id == R.id.btn1 || id == R.id.btn2 || id == R.id.btn3 || id == R.id.btn4 || id == R.id.btn5 || id == R.id.btn6 || id == R.id.btn7 || id == R.id.btn8 || id == R.id.btn9 || id == R.id.btnDot || id == R.id.btnPlus || id == R.id.btnMinus || id == R.id.btnMultiply || id == R.id.btnDevide) {tvResult.setText(tvText + btnText);} else if (id == R.id.btnEqual) {// 计算结果Pattern p = Pattern.compile("(\\d+)([\\+\\-\\*\\/])(\\d+)");Matcher m = p.matcher(tvText);if (m.find()) {double d1 = Double.parseDouble(m.group(1));double d2 = Double.parseDouble(m.group(3));double d3 = 0;if ("+".equals(m.group(2))) {d3 = d1 + d2;}if ("-".equals(m.group(2))) {d3 = d1 - d2;}if ("*".equals(m.group(2))) {d3 = d1 * d2;}if ("/".equals(m.group(2))) {d3 = d1 / d2;}tvResult.setText(tvText + btnText + d3);}} else if (id == R.id.btnBackSpace) {if (tvResult.getText().toString().length() != 0) {tvResult.setText(tvResult.getText().toString().substring(0, tvResult.getText().toString().length() - 1));}} else {throw new IllegalStateException("Unexpected value: " + v.getId());}}
}

3、运行测试

将项目在AVD上运行,测试其是否符合需求分析中的要求。

在这里插入图片描述

注意点

你的软件的API 要比我原本的高或相等才能正常运行;
在这里插入图片描述

结束~

相关文章:

【Android嵌入式开发及实训课程实验】【项目1】 图形界面——计算器项目

【项目1】 图形界面——计算器项目 需求分析界面设计实施1、创建项目2、 界面实现实现代码1.activity_main.xml2.Java代码 - MainActivity.java 3、运行测试 注意点结束~ 需求分析 开发一个简单的计算器项目&#xff0c;该程序只能进行加减乘除运算。要求界面美观&#xff0c;…...

利用SPSS进行神经网络分析过程及结果解读

模拟人类实际神经网络的数学方法问世以来&#xff0c;人们已慢慢习惯了把这种人工神经网络直接称为 神经网络。 神经网络在系统辨识、模式识别、智能控制等领域有着广泛而吸引人的前景&#xff0c;特别在智能控制中&#xff0c;人们对神经网络的自学习功能尤其感兴趣&#xff0…...

聚观早报 |东方甄选将上架文旅产品;IBM首台模块化量子计算机

【聚观365】12月6日消息 东方甄选将上架文旅产品 IBM首台模块化量子计算机 新思科技携手三星上新兴领域 英伟达与软银推动人工智能研发 苹果对Vision Pro供应商做出调整 东方甄选将上架文旅产品 东方甄选宣布12月10日将在东方甄选APP上线文旅产品&#xff0c;受这一消息影…...

web服务器之——www服务器的基本配置

目录 一、www简介 1、什么是www 2、www所用的协议 3、WEB服务器 4、主要数据 5、浏览器 二、 网址及HTTP简介 1、HTTP协议请求的工作流程 三、www服务器的类型(静态网站&#xff08;HTML&#xff09;&#xff0c; 动态网站(jsp python,php,perl)) 1、 仅提供…...

微信小程序 -- ios 底部小黑条样式问题

问题&#xff1a; 如图&#xff0c;ios有的机型底部伪home键会显示在按钮之上&#xff0c;导致点击按钮的时候误触 解决&#xff1a; App.vue <script>export default {wx.getSystemInfo({success: res > {let bottomHeight res.screenHeight - res.safeArea.bott…...

白盒测试:探索软件内部结构的有效方法

引言&#xff1a; 在软件开发过程中&#xff0c;测试是确保软件质量的关键环节。传统的黑盒测试方法主要关注软件的功能和外部行为&#xff0c;而忽略了软件的内部结构和实现细节。然而&#xff0c;随着软件复杂性的增加&#xff0c;仅仅依靠黑盒测试已经无法满足项目的需求。因…...

图论-并查集

并查集(Union-find Sets)是一种非常精巧而实用的数据结构,它主要用于处理一些不相交集合的合并问题.一些常见的用途有求连通子图,求最小生成树Kruskal算法和最近公共祖先(LCA)等. 并查集的基本操作主要有: .1.初始化 2.查询find 3.合并union 一般我们都会采用路径压缩 这样…...

redis-学习笔记(Jedis 通用命令)

flushAll 清空全部的数据库数据 jedis.flushAll();set & get set 命令 get 命令 运行结果展示 exists 判断该 key 值是否存在 当 redis 中存在该键值对时, 返回 true 如果键值对不存在, 返回 false keys 获取所有的 key 值 参数是模式匹配 *代表匹配任意个字符 _代表匹配一…...

C语言:高精度乘法

P1303 A*B Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 第一次画图&#xff0c;略显简陋。 由图可以看出c的小标与x,y下标的关系为x的下标加上y的下标再减一。 由此得到&#xff1a; c [ i j - 1 ] x [ i ] * y [ j ]x #include<stdio.h> #include<st…...

UE4 Niagara学习笔记

需要在其他发射器的同一个粒子位置发射其他粒子就用Spawn Particles from other Emitter 把发射器名字填上去即可 这里Move to Nearest Distance Field Subface GPU&#xff0c;可以将生成的Niagara附着到最近的物体上 使用场景就是做的火苗附着到物体上...

多维时序 | Matlab实现GA-LSTM-Attention遗传算法优化长短期记忆神经网络融合注意力机制多变量时间序列预测

多维时序 | MATLAB实现BWO-CNN-BiGRU-Multihead-Attention多头注意力机制多变量时间序列预测 目录 多维时序 | MATLAB实现BWO-CNN-BiGRU-Multihead-Attention多头注意力机制多变量时间序列预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 多维时序 | Matlab实…...

LeetCode205. Isomorphic Strings

文章目录 一、题目二、题解 一、题目 Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character wh…...

Event Driven设计模式

EDA&#xff08;Event-Driven Architecture&#xff09;是一种实现组件之间松耦合、易扩展的架构方式。一个最简单的EDA设计需要包含如下几个组件&#xff1a; Events&#xff1a;需要被处理的数据。一个Event至少包含两个属性&#xff0c;类型和数据&#xff0c;类型决定了Eve…...

PostgreSql 设置自增字段

一、概述 序列类型是 PostgreSQL 特有的创建一个自增列的方法。包含 smallserial、serial和 bigserial 类型&#xff0c;它们不是真正的类型&#xff0c;只是为了创建唯一标识符列而存在的方便符号。其本质也是调用的序列&#xff0c;序列详情可参考&#xff1a;《PostgreSql 序…...

什么是泊松图像混合

泊松图像混合&#xff08;Poisson Image Editing&#xff09;的原理基于泊松方程。该方法旨在保持图像中的梯度一致性&#xff0c;从而在图像编辑中实现平滑和无缝的混合。以下是泊松图像混合的基本原理和公式&#xff1a; 泊松方程 泊松方程是一个偏微分方程&#xff0c;通常…...

OpenAI 承认 ChatGPT 最近确实变懒,承诺修复问题

文章目录 一. ChatGPT 指令遵循能力下降引发用户投诉1.1 用户抱怨回应速度慢、敷衍回答、拒绝回答和中断会话 二. OpenAI 官方确认 ChatGPT 存在问题&#xff0c;展开调查三. OpenAI 解释模型行为差异&#xff0c;回应用户质疑四. GPT-4 模型变更受人事动荡和延期影响 一. Chat…...

创作活动(四十九)———低代码:美味膳食或垃圾食品?

#低代码&#xff1a;美味膳食或垃圾食品&#xff1f;# 一、什么是低代码 低代码是一种开发方法&#xff0c;通过可视化界面和少量的编码&#xff0c;使开发者能够快速构建应用程序。它的目标是提高开发效率、降低开发成本&#xff0c;并支持快速迭代和敏捷开发。 二、低代码的…...

【DL-TensorFlow遇错】TensorFlow中遇错合集

TensorFlow中遇错合集 一、AttributeError: module tensorflow has no attribute placeholder二、RuntimeError: tf.placeholder() is not compatible with eager execution. 一、AttributeError: module tensorflow has no attribute placeholder 错误原因 tensorflow版本问…...

pymysql代替mysqlclient,解决mysqlclient因版本不兼容无法安装成功而无法连接mysql的问题

pymysql代替mysqlclient&#xff0c;解决mysqlclient因版本不兼容无法安装成功而无法连接mysql的问题 原因&#xff1a;版本或者环境兼容问题&#xff0c;导致如centos或者其他Linux无法安装mysqlclient模块 解决办法&#xff1a;安装pymysql作为替代 在Django中连接MySQL数…...

uni-app 设置当前page界面进入直接变为横屏模式

首先 我们打开项目的 manifest.json 在左侧导航栏中找到 源码视图 然后找到 app-plus 配置 在下面加上 "orientation": [//竖屏正方向"portrait-primary",//竖屏反方向"portrait-secondary",//横屏正方向"landscape-primary",//横屏…...

Mysql的多表联合查询

内连接 隐式内连接 select column from tb1,tb2 where 条件; 显示内连接 关键字&#xff1a;[inner] join on 显示内连接与外连接的不同是新增的关键字&#xff0c;inner join 以及 使用on 替换了where select column from tb1 [inner] join tb2 on 条件; 外连接 左外…...

Linux上使用Python的requests库进行HTTP请求

在Linux上使用Python的requests库进行HTTP请求是一种非常方便和高效的方式。requests库是一个第三方库&#xff0c;用于发送HTTP请求并获取响应。下面是一个简单的示例&#xff0c;演示如何使用requests库发送GET请求并获取响应。 首先&#xff0c;你需要安装requests库。你可…...

图像处理领域的应用

图像处理领域的应用 文章目录 图像处理领域的应用1.图像类型2.图像转换3.彩色图像表示模式4.图像变换5.图像增强 1.图像类型 #mermaid-svg-x6mNS3Y1YkPvWUsQ {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-x6mNS3Y1…...

MySQL笔记-第18章_MySQL8其它新特性

视频链接&#xff1a;【MySQL数据库入门到大牛&#xff0c;mysql安装到优化&#xff0c;百科全书级&#xff0c;全网天花板】 文章目录 第18章_MySQL8其它新特性1. MySQL8新特性概述1.1 MySQL8.0 新增特性1.2 MySQL8.0移除的旧特性 2. 新特性1&#xff1a;窗口函数2.1 使用窗口…...

C语言—每日选择题—Day46

第一题 1. 下列程序段的输出结果是&#xff08;&#xff09; #include <stdio.h> int main() {int x 1,a 0,b 0;switch(x) {case 0: b;case 1: a;case 2: a;b;}printf("a%d,b%d\n", a, b);return 0; } A&#xff1a;a2,b1 B&#xff1a;a1,b1 C&#xf…...

flex布局,换行的元素上下设置间距

要生成的效果图如下&#xff1a; display:flexflex-direction: row;flex-wrap: wrap;当我们使用弹性盒子布局后&#xff0c;默认元素是没有外边距的&#xff0c;紧挨着样式就有点丑&#xff0c;如果想使换行后&#xff0c;元素的外边距有个距离&#xff0c;可以用如下方法解决…...

【智能家居】八、监控摄像采集、人脸识别比对进行开门功能点

一、使用 fswebcam 测试 USB 摄像头 二、根据demo来实现功能点 三、功能点编写编译运行实现 四、mjpg实现监控识别 五、V4L2 视频设备 Linux 内核模块的一部分 一、使用 fswebcam 测试 USB 摄像头 a. 安装 fswebcam orangepiorangepi:~$ sudo apt update orangepiorangepi:~…...

golang的文件操作

获取文件列表路径 package _caseimport ("fmt""log""os""strings" )// 获取文件路径 // 源文件目录 const sourceDir "file/"// 目标文件目录 const destDir "det_file/"// 拿到目录下完整的路径 func geFiles…...

数据库版本管理框架-Flyway(从入门到精通)

一、flyway简介 Flyway是一个简单开源数据库版本控制器&#xff08;约定大于配置&#xff09;&#xff0c;主要提供migrate、clean、info、validate、baseline、repair等命令。它支持SQL&#xff08;PL/SQL、T-SQL&#xff09;方式和Java方式&#xff0c;支持命令行客户端等&am…...

外网访问内网服务器使用教程

如何在任何地方都能访问自己家里的笔记本上的应用&#xff1f;如何让局域网的服务器可以被任何地方访问到&#xff1f;有很多类似的需求&#xff0c;我们可以统一用一个解决方案&#xff1a;内网穿透。内网穿透的工具及方式有很多&#xff0c;如Ngrok、Ssh、autossh、Natapp、F…...