Android:简易计算器

article/2025/11/2 9:41:31

Android 计算器的简易实现

Github

私以为这个小项目中最难的是逻辑部分
目前资瓷加、减、乘、除、乘方,括号这些操作符
其中减号可以当做负号使用

AC长按清屏

特色:实时结果 数字分隔(100,000)
支持hentai算式 如3×-(-(-6+5)×2)

不足:还未加入自动调节字体大小功能
连续括号问题存在bug

图标

UI


预备知识

Android:控件中Button、Textview / EditText 的使用及布局方式
Java:集合,String及数组的熟练使用
算法:中缀转后缀及后缀表达式的计算 及复杂表达式的处理


UI

我的丑丑的UI是仿写手机中的一个计算器的界面,右边青色的的条条是可以划出来使用很多计算符的。但是还没学到,就先做个条条…

界面使用嵌套的线性布局实现

嵌套线性布局的使用技巧:先把布局块分好,再写控件,不然嵌套的多了,自己就容易写乱

上xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:weightSum="8"><LinearLayout
        android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="3"android:orientation="vertical"android:weightSum="5"><EditText
                android:id="@+id/text_screen"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="3"android:layout_gravity="right"android:gravity="right"android:focusable="false"android:cursorVisible="false"android:singleLine="true"android:background="@null"android:textColor="#666666"android:textSize="60sp" /><EditText
                android:id="@+id/text_result"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2"android:layout_gravity="right"android:gravity="right"android:focusable="false"android:cursorVisible="false"android:singleLine="true"android:background="@null"android:textColor="#999999"android:textSize="40sp" /></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="5"android:weightSum="10"android:orientation="horizontal"><LinearLayout
            android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="7"android:orientation="vertical"android:weightSum="5"><LinearLayout
                android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"android:weightSum="3"><Button
                    android:id="@+id/btn_leftBracket"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="("android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Button
                    android:id="@+id/btn_rightBracket"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text=")"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Button
                    android:id="@+id/btn_power"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="^"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout><LinearLayout
                android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:weightSum="3"><Button
                    android:id="@+id/btn_1"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="1"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Button
                    android:id="@+id/btn_2"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="2"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Button
                    android:id="@+id/btn_3"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="3"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout><LinearLayout
                android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:weightSum="3"><Button
                    android:id="@+id/btn_4"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="4"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Button
                    android:id="@+id/btn_5"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="5"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Button
                    android:id="@+id/btn_6"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="6"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout><LinearLayout
                android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:weightSum="3"><Button
                    android:id="@+id/btn_7"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="7"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Button
                    android:id="@+id/btn_8"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="8"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Button
                    android:id="@+id/btn_9"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="9"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout><LinearLayout
                android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:weightSum="3"><Button
                    android:id="@+id/btn_del"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="."android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Button
                    android:id="@+id/btn_0"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="0"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Button
                    android:id="@+id/btn_calculate"android:background="#333333"android:textColor="#FFFFFF"android:textSize="26sp"android:text="="android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout></LinearLayout><LinearLayout
            android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="3"android:weightSum="4"><LinearLayout
                android:orientation="vertical"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="3"android:weightSum="5"><Button
                    android:id="@+id/btn_clear"android:background="#666666"android:textColor="#FFFFFF"android:textSize="26sp"android:text="AC"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"/><Button
                    android:id="@+id/btn_devide"android:background="#666666"android:textColor="#FFFFFF"android:textSize="26sp"android:text="÷"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"/><Button
                    android:id="@+id/btn_multiply"android:background="#666666"android:textColor="#FFFFFF"android:textSize="26sp"android:text="×"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"/><Button
                    android:id="@+id/btn_minus"android:background="#666666"android:textColor="#FFFFFF"android:textSize="26sp"android:text="-"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"/><Button
                    android:id="@+id/btn_plus"android:background="#666666"android:textColor="#FFFFFF"android:textSize="26sp"android:text="+"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"/></LinearLayout><LinearLayout
                android:background="#00FFFC"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"></LinearLayout></LinearLayout></LinearLayout></LinearLayout>

逻辑

写了两个工具类
StringExam类辅助字符串处理,Calculate类进行计算

计算思想,从文本框中取得字符串,检查处理,重点处理– ( )×–÷–,转为后缀表达式再计算

代码太长Github自取


UI与逻辑的交互代码

看到好多学长制造Button注册监听器就写了几十行….
注册监听器可以跟findViewById写到一起
其实看一下R.java中的代码,连着写的控件,id是连着的。而且这部分代码是可修改的。所以用int类型的数就可以批量注册

后面的 switch case也是同理,数字部分明明可以合并,略去好多重复代码

package com.example.hp.calculator;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;import java.text.Format;import static com.example.hp.calculator.Calculate.arrange;
import static com.example.hp.calculator.StringExam.*;public class InteractActivity extends AppCompatActivity implements View.OnClickListener{private StringBuffer text = new StringBuffer();private TextView edit_screen;private TextView result_screen;private String s = new String();private boolean flag = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.module_activity_main);//0x7f070022 ~0032int id = 0x7f070022;for(id=0x7f070022;id<=0x7f070034;id++){findViewById(id).setOnClickListener(this);}findViewById(R.id.btn_clear).setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View view) {text = new StringBuffer();s = "";edit_screen.setText("0");result_screen.setText("");return false;}});edit_screen = findViewById(R.id.text_screen);result_screen = findViewById(R.id.text_result);findViewById(R.id.btn_leftBracket).setOnClickListener(this);findViewById(R.id.btn_rightBracket).setOnClickListener(this);
//        edit_screen.setMovementMethod(ScrollingMovementMethod.getInstance());}@Overridepublic void onClick(View view) {int textLen = text.length();switch (view.getId()){case R.id.btn_0:if(flag){text = new StringBuffer();textLen = 0;}if(textLen==0||text.charAt(textLen-1)!=')')text.append('0');break;case R.id.btn_1://0x7f070022case R.id.btn_2:case R.id.btn_3:case R.id.btn_4:case R.id.btn_5:case R.id.btn_6:case R.id.btn_7:case R.id.btn_8:case R.id.btn_9:if(flag){text = new StringBuffer();textLen = 0;}if(textLen==0||text.charAt(textLen-1)!=')')text.append((char) ('1'+view.getId()-0x7f070023));break;//根据id判断所加字符case R.id.btn_del:if(textLen==0)text.append("0.");else if(isNum(text.charAt(textLen-1))&&judgeDel(text.toString())==false)text.append('.');break;case R.id.btn_leftBracket:if(textLen==0||(textLen>0&&text.charAt(textLen-1)!='.'&&isNum(text.charAt(textLen-1))==false))text.append('(');break;case R.id.btn_rightBracket:if(cntBrackets(text.toString())>0&&textLen>0&&!isOperation(text.charAt(textLen-1))&&text.charAt(textLen-1)!='.'&&text.charAt(textLen-1)!='(')text.append(')');break;case R.id.btn_plus://if(textLen>0&&!isOperation(text.charAt(textLen-1))&&text.charAt(textLen-1)!='.')if(textLen>0&&(isNum(text.charAt(textLen-1))||text.charAt(textLen-1)==')'))text.append('+');else if(textLen>0&&cntCharacter(text.toString())==1)text.setCharAt(textLen-1,'+');break;case R.id.btn_minus:
//             if(textLen==0||(text.charAt(textLen-1)!='+'&&text.charAt(textLen-1)!='-'&&text.charAt(textLen-1)!='.'))if(textLen==0)text.append('-');else if(textLen==1){if(text.charAt(textLen-1)!='-')text.append('-');}else if(textLen == 2&&text.charAt(0)=='('){if(text.charAt(1)!='-')text.append('-');}else if(text.charAt(textLen-1)!='.'&&cntCharacter(text.toString())<2||(cntCharacter(text.toString())==2&&(text.charAt(textLen-1)!='-'&&text.charAt(textLen-2)!='-'))||text.charAt(textLen-1)=='(')text.append('-');
//                if(textLen>0){
//                    Toast.makeText(this,String.valueOf(text.charAt(textLen-1)!='.'&&cntCharacter(text.toString())<=2||(textLen==1&&text.charAt(textLen-1)!='-')),Toast.LENGTH_SHORT).show();
//                }break;case R.id.btn_multiply:if(textLen>0&&(isNum(text.charAt(textLen-1))||text.charAt(textLen-1)==')'))text.append('×');else if(textLen>0&&cntCharacter(text.toString())==1)text.setCharAt(textLen-1,'×');break;case R.id.btn_devide:if(textLen>0&&(isNum(text.charAt(textLen-1))||text.charAt(textLen-1)==')'))text.append('÷');else if(textLen>0&&cntCharacter(text.toString())==1)text.setCharAt(textLen-1,'÷');break;case R.id.btn_power:if(textLen>0&&(isNum(text.charAt(textLen-1))||text.charAt(textLen-1)==')'))text.append('^');else if(cntCharacter(text.toString())==1){text.setCharAt(textLen-1,'^');}break;case R.id.btn_clear:if(textLen>0)text.deleteCharAt(textLen-1);if(text.length()==0)s="";break;case R.id.btn_calculate:if(!edit_screen.getText().equals(result_screen.getText())){flag = true;}try{text = new StringBuffer(s);//嘻嘻嘻edit_screen.setText(devideByDel(s));result_screen.setText("");} catch (Exception e){edit_screen.setText("bug發現,請聯繫開發者kafm。QQ1002605741");text = new StringBuffer();s = "";}break;}if(view.getId()!=R.id.btn_calculate){flag = false;
//            String tem = devideByDel();edit_screen.setText(parseStringAndDevideByDel(text.toString()));//可用正则表达式优化if(text.length()>0)s = completeString(text.toString());if(s.length()>0) {try {s=arrange(s);} catch (infinityException e) {s = "∞";} catch (Exception e) {
//坑}}result_screen.setText(devideByDel(s));}}
}

2018/8/4


http://chatgpt.dhexx.cn/article/fs7ri8Xn.shtml

相关文章

Android——一个神奇的计算器APP

一个神奇的计算器APP 中缀运算效果视频自定义圆形TextView效果图建立attr文件绘制圆形 字符拼接清空栈内元素 运算执行手势监听入栈&&出栈运算结果小数位判断运算代码 任意进制转换效果视频进制转换结果逆置结果返回 中缀运算 中缀运算定义了两个栈&#xff0c;数字栈和…

【Android】简易计算器

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 Android简易计算器 前言一、Kotlin版本1. activity_main.xml2. MainActivity.kt 二、Java版本1. activity_main.xml2. MainActivity.java 总结 前言 提示&#xff1a;Kotlin…

Android 计算器

Android 计算器 开发环境&#xff1a;android stdio 3.6.1 最终效果&#xff1a; 竖屏&#xff1a; 横屏&#xff1a; 完整项目&#xff1a;百度网盘链接 密码 v89n 主要代码&#xff1a; MainActivity.java: package com.example.experience_two;import androidx.appc…

android计算器

1、效果预览 2、项目布局 3、main.xml文件内容 <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:layout_width"fill_parent"android:layout_height"fi…

Android计算器简单实现及代码分析

一、UI布局及代码 页面效果 布局代码 <?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"…

Android计算器——入门

Android计算器—入门 作者&#xff1a;黑衣侠客 一.前言 这是我写的第一个App&#xff0c;利用的是《安卓第一行代码》第三章&#xff0c;UI控件的一些知识&#xff0c;然后整体结构综合了一些CSDN博客和简书上的一些著作&#xff0c;同样&#xff0c;在写Android计算器&…

基础复习——项目练习——计算器

布局&#xff1a; <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_height"match_parent"android:background"#eeeeee"android:orientation"vert…

安卓-AndroidStudio-计算器

安卓--Android Studio--简易计算器 结果截图关键代码讲解完整代码 本人是一名在校学生&#xff0c;由于上个学期的课程内容包括移动开发&#xff0c;其中的实验也算是大部分是自己完成的&#xff0c;所以算是写博客记录一下吧。 第一次写&#xff0c;有啥问题及时交流哈&#x…

Django数据库操作

目录 一、Django/ORM框架介绍及配置 1.1、ORM框架介绍 1.2、Django数据库配置 二、定义模型类数 2.1、定义模型类 2.2、迁移 2.3、插入数据 三、单表据库操作(增、删、改、查) 3.1、增 3.2、删 3.3、改 3.4查&#xff08;重点&#xff09; 四、两表联查 4.1、一对…

Qt入门------数据库操作

文章目录 一、数据库1.数据库驱动2.查询驱动3.连接数据库4.执行sql语句5.插入数据6.事务 二 &#xff0c;sql模型类1.QSqlQueryModel模型2.QSqlTableModel模型3.QSqlRelationalTableModel模型 一、数据库 Qt中的Qt SQL模块提供了对数据库的支持&#xff0c;模块中类可分为三层…

Java的数据库操作

Java的数据库操作&#xff0c;上学期末的课设是用到的&#xff0c;然而老师把JDBC连接mysql数据库的代码和配置文件已经给好了&#xff0c;那时对它可以说是只有一点点印象。 今天跟着书走一边敲一边&#xff0c;总算是有所进步。 数据库表的创建和初始数据的操作&#xff0c…

一文详解python中的数据库操作

python中的数据库操作 一、数据库编程接口1. 连接对象 二、使用内置的SQLite1.创建数据库文件2.操作SQLite 三、MySql数据库的使用3.1 安装MySql3.2 设置环境变量3.3 启动MySql3.4 使用navicat for mysql 管理软件3.5 安装PyMysql模块3.6 连接数据库 四、创建数据表4.1 创建boo…

数据库操作练习题

如何查看员工表中的所有字段&#xff1f; 如何查看员工表中的姓名和性别&#xff1f; 如何知道每个员工一年的总收入&#xff1f; 怎么查看女员工的信息&#xff1f; 如何查看月薪范围位于 8000 到 12000 之间的员工&#xff1f; 查询员工中叫做“张三”、“李四” 或“张飞”的…

MySQL(一)数据库操作

1.关系型数据库与非关系型数据库区别与联系 1.关系型数据库 ( SQL ) MySQL , Oracle , SQL Server , SQLite , DB2 , … 关系型数据库通过外键关联来建立表与表之间的关系 2.非关系型数据库 ( NOSQL ) Redis , MongoDB , … 非关系型数据库通常指数据以对象的形式存储在数据…

使用ORACLE 进行数据库操作

上一期讲了 如何创建表&#xff0c;例如我们创建了一个 名为 t_customer 的表&#xff0c;格式为下&#xff1a; create table t_customer(cust_id int primary key,cust_name varchar(20),cust_gender char(3),cust_age int,cust_birthday date,cust_height number(3,2),cust…

C#数据库操作

功能需求 1&#xff0c;利用随机数模拟产生每次考试成绩 2&#xff0c;将每次考试成绩存入到数据库 3&#xff0c;将每次考试成绩划分优、良、中、差、不及格五类&#xff0c;并作为查询条件&#xff0c;查询符合每种水平的成绩 技术知识点 1.random类的使用 2.数据库的链…

云数据库操作

1 云数据库创建 每一个list就相当于一个表&#xff0c;每个记录就是一条内容 云数据库存储形式是JSON2 读取云数据库值 数据库初始化 const db wx.cloud.database() 连接数据库 db.collection(需要连接的数据列表名)可以通过.doc(‘具体id号’)获取具体的信息内容获取年、月…

Android数据库操作

Android内置了一个名为SQLite的关系型数据库&#xff0c;这是一款轻量型的数据库&#xff0c;操作十分简便。SQLite与别的数据库不同的是&#xff0c;它没有数据类型。可以保存任何类型的数据到你所想要保存的任何表的任何列中。但它又支持常见的类型比如: NULL, VARCHAR, TEXT…

数据库基本操作

一、数据库基本操作 1.数据库的基本操作 -- 1.数据库的基本操作 使用test数据库 USE test; -- 查看当前test数据库中所有表 MySQL命令 SHOW TABLES; -- 查表的基本信息 SHOW CREATE TABLE student; -- -- 查看表的字段信息 desc student; 2.数据表的基本操作 -- 2.数据表的…

数据库基础操作

一、数据库的操作 数据库与客户端是通过网络进行交互的。 1、显示当前数据库 sql语句必须以 ; 结尾 show databases; 2、创建数据库 create database 数据库名; ->如果数据库sql敲错了&#xff0c;会有提示。 ->如果sql敲错了&#xff0c;可以按 ctrlc来终止sql。…