当前位置: 首页 > 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",//横屏…...

FFmpeg 低延迟同屏方案

引言 在实时互动需求激增的当下&#xff0c;无论是在线教育中的师生同屏演示、远程办公的屏幕共享协作&#xff0c;还是游戏直播的画面实时传输&#xff0c;低延迟同屏已成为保障用户体验的核心指标。FFmpeg 作为一款功能强大的多媒体框架&#xff0c;凭借其灵活的编解码、数据…...

【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件&#xff08;System Property Definition File&#xff09;&#xff0c;用于声明和管理 Bluetooth 模块相…...

零基础设计模式——行为型模式 - 责任链模式

第四部分&#xff1a;行为型模式 - 责任链模式 (Chain of Responsibility Pattern) 欢迎来到行为型模式的学习&#xff01;行为型模式关注对象之间的职责分配、算法封装和对象间的交互。我们将学习的第一个行为型模式是责任链模式。 核心思想&#xff1a;使多个对象都有机会处…...

Map相关知识

数据结构 二叉树 二叉树&#xff0c;顾名思义&#xff0c;每个节点最多有两个“叉”&#xff0c;也就是两个子节点&#xff0c;分别是左子 节点和右子节点。不过&#xff0c;二叉树并不要求每个节点都有两个子节点&#xff0c;有的节点只 有左子节点&#xff0c;有的节点只有…...

html css js网页制作成品——HTML+CSS榴莲商城网页设计(4页)附源码

目录 一、&#x1f468;‍&#x1f393;网站题目 二、✍️网站描述 三、&#x1f4da;网站介绍 四、&#x1f310;网站效果 五、&#x1fa93; 代码实现 &#x1f9f1;HTML 六、&#x1f947; 如何让学习不再盲目 七、&#x1f381;更多干货 一、&#x1f468;‍&#x1f…...

Linux C语言网络编程详细入门教程:如何一步步实现TCP服务端与客户端通信

文章目录 Linux C语言网络编程详细入门教程&#xff1a;如何一步步实现TCP服务端与客户端通信前言一、网络通信基础概念二、服务端与客户端的完整流程图解三、每一步的详细讲解和代码示例1. 创建Socket&#xff08;服务端和客户端都要&#xff09;2. 绑定本地地址和端口&#x…...

day36-多路IO复用

一、基本概念 &#xff08;服务器多客户端模型&#xff09; 定义&#xff1a;单线程或单进程同时监测若干个文件描述符是否可以执行IO操作的能力 作用&#xff1a;应用程序通常需要处理来自多条事件流中的事件&#xff0c;比如我现在用的电脑&#xff0c;需要同时处理键盘鼠标…...

Python 实现 Web 静态服务器(HTTP 协议)

目录 一、在本地启动 HTTP 服务器1. Windows 下安装 node.js1&#xff09;下载安装包2&#xff09;配置环境变量3&#xff09;安装镜像4&#xff09;node.js 的常用命令 2. 安装 http-server 服务3. 使用 http-server 开启服务1&#xff09;使用 http-server2&#xff09;详解 …...

上位机开发过程中的设计模式体会(1):工厂方法模式、单例模式和生成器模式

简介 在我的 QT/C 开发工作中&#xff0c;合理运用设计模式极大地提高了代码的可维护性和可扩展性。本文将分享我在实际项目中应用的三种创造型模式&#xff1a;工厂方法模式、单例模式和生成器模式。 1. 工厂模式 (Factory Pattern) 应用场景 在我的 QT 项目中曾经有一个需…...

麒麟系统使用-进行.NET开发

文章目录 前言一、搭建dotnet环境1.获取相关资源2.配置dotnet 二、使用dotnet三、其他说明总结 前言 麒麟系统的内核是基于linux的&#xff0c;如果需要进行.NET开发&#xff0c;则需要安装特定的应用。由于NET Framework 是仅适用于 Windows 版本的 .NET&#xff0c;所以要进…...