【Android学习】简易计算器的实现

news/2024/9/20 8:57:19/ 标签: android, 学习, gitee

 1.项目基础目录

新增dimens.xml 用于控制全部按钮的尺寸。图片资源放在drawable中。

 另外 themes.xml中原来的

    <style name="Theme.Learn" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

 变为了,加上后可针对button中增加图片和文字修改背景。

    <style name="Theme.Learn" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">

2. XML代码

UI效果

<?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:background="#888888"android:orientation="vertical"><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:gravity="center"android:text="@string/calTitle"android:textColor="@color/black"android:textSize="17sp" /><TextViewandroid:id="@+id/tv_res"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="8dp"android:layout_marginTop="5dp"android:layout_marginRight="8dp"android:background="#FFFFFF"android:gravity="right|bottom"android:lines="3"android:text="0"android:textColor="@color/black"android:textSize="25sp" /><GridLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:columnCount="4"android:rowCount="5"android:layout_marginTop="10dp"><Buttonandroid:id="@+id/btn_ce"android:text="@string/CE"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_multi"android:text="@string/multi"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_divide"android:text="@string/divide"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_back"android:text="@string/back"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_seven"android:text="@string/seven"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_eight"android:text="@string/eight"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_nine"android:text="@string/nine"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_plus"android:text="@string/plus"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_four"android:text="@string/four"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_five"android:text="@string/five"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_six"android:text="@string/six"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_minus"android:text="@string/minus"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_one"android:text="@string/one"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_two"android:text="@string/two"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_three"android:text="@string/three"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><ImageButtonandroid:id="@+id/btn_radical"android:src="@drawable/radical"android:scaleType="centerInside"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck"/><Buttonandroid:id="@+id/btn_fraction"android:text="@string/fraction"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_zero"android:text="@string/zero"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_point"android:text="@string/point"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_equal"android:text="@string/equal"android:layout_width="0dp"android:layout_height="@dimen/btn_height"android:layout_columnWeight="1"android:gravity="center"android:textSize="@dimen/btn_font_size"android:textColor="@color/black"/></GridLayout></LinearLayout></ScrollView></LinearLayout>

3.Java代码

package com.example.learn;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class CalActivity extends AppCompatActivity implements View.OnClickListener {//运算符private String operator = "";//第一个操作数private String firstNum = "";//第二个操作数private String secondNum = "";//结果private String result = "";//显示的文本内容private String showText = "";//记录结果private TextView tv_res;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_cal);//发现页面控件tv_res = findViewById(R.id.tv_res);//Button 组件//1 运算符findViewById(R.id.btn_back).setOnClickListener(this);findViewById(R.id.btn_ce).setOnClickListener(this);findViewById(R.id.btn_plus).setOnClickListener(this);findViewById(R.id.btn_minus).setOnClickListener(this);findViewById(R.id.btn_multi).setOnClickListener(this);findViewById(R.id.btn_divide).setOnClickListener(this);findViewById(R.id.btn_radical).setOnClickListener(this);findViewById(R.id.btn_equal).setOnClickListener(this);findViewById(R.id.btn_fraction).setOnClickListener(this);findViewById(R.id.btn_point).setOnClickListener(this);//2、数字findViewById(R.id.btn_zero).setOnClickListener(this);findViewById(R.id.btn_one).setOnClickListener(this);findViewById(R.id.btn_two).setOnClickListener(this);findViewById(R.id.btn_three).setOnClickListener(this);findViewById(R.id.btn_four).setOnClickListener(this);findViewById(R.id.btn_five).setOnClickListener(this);findViewById(R.id.btn_six).setOnClickListener(this);findViewById(R.id.btn_seven).setOnClickListener(this);findViewById(R.id.btn_eight).setOnClickListener(this);findViewById(R.id.btn_nine).setOnClickListener(this);}@Overridepublic void onClick(View view) {String inputText = "";//处理图片按钮if (view.getId() == R.id.btn_radical) {inputText = "#";} else {inputText = ((Button) view).getText().toString();}switch (view.getId()) {//清空按钮case R.id.btn_ce:initZero();break;//回退按钮case R.id.btn_back:clear();break;//四则运算符case R.id.btn_plus:case R.id.btn_minus:case R.id.btn_divide:case R.id.btn_multi:operator = inputText;show(showText + operator);break;//求导数case R.id.btn_fraction:result = String.valueOf(getFraction());//刷新结果refreshRes(result);//显示show(showText + "/=" +result);break;//求开方case R.id.btn_radical:result = String.valueOf(getRadical());//刷新结果refreshRes(result);//显示show(showText + "/=" +result);break;//等号case R.id.btn_equal:result = String.valueOf(getRes());//刷新结果refreshRes(result);//显示show(showText + "=" +result);break;default://无运算符 则输入数字为第一个数if (operator.equals("")) {firstNum = firstNum + inputText;} else {secondNum = secondNum + inputText;}//判断是否首先输入了0或者小数点if (showText.equals("0") && !inputText.equals(".")) {show(inputText);} else {//显示输入的操作数show(showText + inputText);}break;}}private double getRadical() {return Math.sqrt(Double.parseDouble(firstNum));}private double getFraction() {return 1.0 / Double.parseDouble(firstNum);}private void initZero() {showText = "0";operator = "";firstNum = "";secondNum = "";tv_res.setText(showText);}//清空操作private void clear() {showText = "";operator = "";firstNum = "";secondNum = "";tv_res.setText(showText);}//show inputprivate void show(String text) {showText = text;tv_res.setText(showText);}private double getRes() {switch (operator) {case "+":return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);case "-":return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);case "*":return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);default:return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);}}//运行equal后刷新状态private void refreshRes(String new_res) {result = new_res;firstNum=result;secondNum="";operator="";}}

以上代码可实现简易计算器的运算,但存在Bug,但是简单的计算是没问题。


http://www.ppmy.cn/news/1451955.html

相关文章

Angular中的管道(Pipe)

Angular中的管道(Pipe) 文章目录 Angular中的管道(Pipe)前言一、内置管道1. date管道格式化日期2. currency管道格式化货币3. uppercase和lowercase管道转换字符串大小写4. 小数位数5. JavaScript 对象序列化6. slice7. 管道链 二、自定义管道 前言 Angular中的管道&#xff0…

[蓝桥杯2024]-PWN:ezheap解析(堆glibc2.31,glibc2.31下的double free)

查看保护 查看ida 大致就是只能创建0x60大小的堆块&#xff0c;并且uaf只能利用一次 完整exp&#xff1a; from pwn import* #context(log_leveldebug) pprocess(./ezheap2.31)def alloc(content):p.sendlineafter(b4.exit,b1)p.send(content) def free(index):p.sendlineaft…

Qt扫盲-Qt D-Bus概述

Qt D-Bus概述 一、概述二、总线三、相关概念1. 消息2. 服务名称3. 对象的路径4. 接口5. 备忘单 四、调试五、使用Qt D-Bus 适配器1. 在 D-Bus 适配器中声明槽函数1. 异步槽2. 只输入槽3. 输入输出槽4. 自动回复5. 延迟回复 一、概述 D-Bus是一种进程间通信(IPC)和远程过程调用…

ubuntu Qt打包

在Linux 下如何打包免安装的QT程序&#xff1f;-CSDN博客 [教程][Ubuntu][Qt]将Qt程序打包成deb文件&#xff0c;发布、安装及使用_qt生成deb-CSDN博客

前端关于location的方法以属性

目录 一、location是用来做什么的&#xff1f; 二、location的属性和方法能实现什么功能&#xff1f; 功能一&#xff1a;获取当前页面的URL信息 功能二&#xff1a;页面跳转 location.assign() 附&#xff1a;其他页面跳转的方法 1.location.href 2.window.open() …

QAnything知识库问答系统离线部署(LLM+RAG)

一、QAnything介绍 &#xff08;一&#xff09;简介 QAnything 是网易有道开源的一个问答系统框架&#xff0c;支持私有化部署和SaaS服务两种调用形式。它能够支持多种格式的文件或数据库&#xff0c;提供准确、快速和可靠的问答体验。目前已支持的文件格式包括PDF、Word、PP…

微信小程序开发:深入实现地图导航功能【含代码示例】

微信小程序开发&#xff1a;深入实现地图导航功能【含代码示例】 一、引言二、准备工作三、集成地图SDK四、实现地图显示五、添加标记点和路线 一、引言 微信小程序作为一种轻量级的应用程序&#xff0c;凭借其无需安装、即用即走的特点&#xff0c;迅速在移动应用市场中占据了…

【软件工程】概要设计

目录 前言软件设计简介概要设计模块化模块化的评价耦合内聚 面向对象设计原则Liskov替换原则&#xff08;LSP&#xff09;开放-封闭原则&#xff08;OCP&#xff09;单一职责原则&#xff08;SRP&#xff09;接口隔离原则&#xff08;ISP&#xff09;依赖倒置原则&#xff08;D…

关于springboot内置tomcat最大请求数配置的一些问题

前言 springboot内置了tomcat。那么一个springboot web应用&#xff0c;最大的请求链接数是多少呢&#xff1f;很早以前就知道这个是有个配置&#xff0c;需要的时候&#xff0c;百度一下即可。但&#xff0c;事实并非如此&#xff0c;有几个问题我想大多数人还真不知道。比如…

基于Sping Boot集成的websocket实现聊天室

Spring Boot整合WebSocket实现聊天室 Spring Boot 提供了 Websocket 组件 spring-boot-starter-websocket&#xff0c;用来支持在 Spring Boot环境下对Websocket 的使用。 下面我们就以多人在线聊天室为例&#xff0c;演示 Spring Boot 是如何整合Websocket 实现服务端消息推…

python(abi)是什么,有什么作用呢

python(abi) 是一个特殊的提供项&#xff0c;用于指定软件包所支持的Python ABI&#xff08;Application Binary Interface&#xff09;版本。 Python ABI是一种约定&#xff0c;用于定义Python解释器和扩展模块之间的二进制接口。它确保了不同版本的Python解释器和扩展模块之…

如何使用提示测试为LLMs构建单元测试?

原文地址&#xff1a;how-to-build-unit-tests-for-llms-using-prompt-testing 确保您的人工智能交付&#xff1a;快速测试完美生成应用程序的基本指南 2024 年 4 月 26 日 如果你曾经编写过软件&#xff0c;你就会知道测试是开发过程中必不可少的一部分。特别是单元测试&#…

JavaWeb--1.Servlet

Servlet&#xff08;基础&#xff09; 1、配置依赖&#xff1a; ​ 在pom.xml文件中加入相关依赖 <dependencies><dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><version>5.0.0&l…

KAN网络认识

首先&#xff0c;这是一个基于柯尔莫哥洛夫-阿诺德表示定理的网络。这个定理指出如果函数f是定义在有界域上的多变量连续函数&#xff08;即最终要拟合的非线性函数是连续的&#xff09;&#xff0c;那么该函数就可以表示为多个单变量、加法连续函数的有线组合。 对于机器学习…

手撸Mybatis(三)——收敛SQL操作到SqlSession

本专栏的源码&#xff1a;https://gitee.com/dhi-chen-xiaoyang/yang-mybatis。 引言 在上一章中&#xff0c;我们实现了读取mapper配置并构造相关的mapper代理对象&#xff0c;读取mapper.xml文件中的sql信息等操作&#xff0c;现在&#xff0c;在上一章的基础上&#xff0c…

【热门话题】Chrome 插件研发详解:从入门到实践

&#x1f308;个人主页: 鑫宝Code &#x1f525;热门专栏: 闲话杂谈&#xff5c; 炫酷HTML | JavaScript基础 ​&#x1f4ab;个人格言: "如无必要&#xff0c;勿增实体" 文章目录 Chrome 插件研发详解&#xff1a;从入门到实践一、引言二、Chrome 插件基础概念…

嵌入式物联网系统软硬件基础知识大全(2)

接口技术 1. Flash存储器 (1)Flash存储器是一种非易失性存储器,根据结构的不同可以将其分为NOR Flash和NAND Flash两种。 (2)Flash存储器的特点: A、区块结构:在物理上分成若干个区块,区块之间相互独立。 B、先擦后写:Flash的写操作只能将数据位从1写成0,不能从…

写自己的c库----小话c语言(19)

Q&#xff1a; 对于c代码&#xff0c;经常可能被c代码使用&#xff0c;所以经常需要使用extern "C"语句&#xff0c;老是写这段代码真是很烦人&#xff0c;有什么好的方法&#xff1f; A&#xff1a; 对于重复代码来说&#xff0c;宏无疑是个很好的方法。 #if defin…

spring高级篇(八)

本篇对Spring MVC 的执行流程做一个简单总结 MVC执行流程总结 当浏览器发送一个请求&#xff0c;例如http://localhost:8080/hello&#xff0c;请求到达服务器后&#xff0c;一般会进行如下操作&#xff1a; 1、首先会经过DispatcherServlet&#xff0c;默认映射路径为 /&…

global IoT SIM解决方案

有任何关于GSMA\IOT\eSIM\RSP\业务应用场景相关的问题&#xff0c;欢迎W: xiangcunge59 一起讨论, 共同进步 (加的时候请注明: 来自CSDN-iot). Onomondo提供的全球IoT SIM卡解决方案具有以下特点和优势&#xff1a; 1. **单一全球配置文件**&#xff1a;Onomondo的SIM卡拥…