【Android开发基础】计算器逻辑层代码补充

news2024/10/7 18:28:55

文章目录

    • 一、引言
    • 二、设计
      • 1、案例
      • 2、算法设计
    • 三、编码
      • 1、UI界面设计
        • (1)按钮样式设计
        • (2)主界面布局设计
      • 2、编码
        • (1)控件初始化
        • (2)事件监听器
    • 四、附件

一、引言

  • 描述:关于六月十二日发表的博客【Android开发基础】SQLite开发复刻通讯录、记事本、计算机,有粉丝向我问最后面的计算器作业有没有逻辑层的代码,这里我会给出具体代码。
  • 难度:初级
  • 效果
    在这里插入图片描述

二、设计

1、案例

对于初学者或算法不好的朋友,我觉得有必要先要看一下这样的一个计算方法。
好像是叫函数嵌套方法(专业名词忘了,如果有知道的可以在评论区告诉大家)

	public static void main(String[] args) {
		System.out.print("输入一个数,计算1~n的数和:");
		Scanner input = new Scanner(System.in);
		int max = input.nextInt();
		System.out.print("结果:" + add(0, 1, max));
	}

	private static int add(int S, int min, int max) {
		if(min <= max) {
			S += min;
			return add(S, ++min, max);
		}
		return S;
	}

在这里插入图片描述

2、算法设计

public class alg {

    public static double parse(String formula) {

        int temp = formula.indexOf("+");
        if (temp != -1) {
            return parse(formula.substring(0, temp)) + parse(formula.substring(temp + 1));
        }

        temp = formula.lastIndexOf("-");
        if (temp != -1) {
            return parse(formula.substring(0, temp)) - parse(formula.substring(temp + 1));
        }

        temp = formula.indexOf("*");
        if (temp != -1) {
            return parse(formula.substring(0, temp)) * parse(formula.substring(temp + 1));
        }

        temp = formula.lastIndexOf("/");
        if (temp != -1) {
            return parse(formula.substring(0, temp)) / parse(formula.substring(temp + 1));
        }

        return Double.parseDouble(formula);
    }

}

三、编码

1、UI界面设计

一个优雅的计算界面可能是功能相似的按钮使用的UI样式是一样的

在这里插入图片描述

(1)按钮样式设计

  • 计算符号

比如,基本的 加减乘除(+、-、*、/)和等于(=)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--设置颜色-->
    <solid android:color="#A7DD4E2E"
        />
    <!--设置圆角-->

    <corners android:radius="90dp"/>

    <size
        android:width="90dp"
        android:height="90dp"/>

</shape>
  • 数字

我观察了一些计算器,还有00这种数字按钮,泰库辣

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--设置颜色-->
    <solid android:color="#A6282828"
        />
    <!--设置圆角-->

    <corners android:radius="90dp"/>

    <size
        android:width="90dp"
        android:height="90dp"/>

</shape>
  • 特殊

比如,AC:清空

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--设置颜色-->
    <solid android:color="#A7989595"
        />
    <!--设置圆角-->

    <corners android:radius="90dp"/>

    <size
        android:width="90dp"
        android:height="90dp" />

</shape>

(2)主界面布局设计

        关于主界面,我对其进行进一步的优化,主要参考多款手机的计算机的样式,才发现这种风格看起来对眼睛非常友好。

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="#6F6A6A">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/jsq_text"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="#FFFFFF"
            android:textSize="25dp"
            android:gravity="right"
            android:layout_marginTop="30dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center">

        <Button
            android:id="@+id/ac"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AC"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape"/>

        <Button
            android:id="@+id/fang"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+/-"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape"/>

        <Button
            android:id="@+id/yu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="%"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape" />

        <Button
            android:id="@+id/chu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_ceng"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <Button
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_hei"/>

        <Button
            android:id="@+id/two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_hei"/>

        <Button
            android:id="@+id/there"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_hei" />

        <Button
            android:id="@+id/jia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_ceng"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <Button
            android:id="@+id/four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_hei"/>

        <Button
            android:id="@+id/five"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_hei"/>

        <Button
            android:id="@+id/six"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_hei" />

        <Button
            android:id="@+id/jiang"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_ceng"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <Button
            android:id="@+id/seven"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_hei"/>

        <Button
            android:id="@+id/event"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_hei"/>

        <Button
            android:id="@+id/nine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_hei" />

        <Button
            android:id="@+id/cheng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*"
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_ceng"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <Button
            android:id="@+id/zero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_margin="10dp"
            android:textSize="25dp"
            android:background="@drawable/shape_ling"/>

        <Button
            android:id="@+id/dian"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="."
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_hei" />

        <Button
            android:id="@+id/dengyu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="="
            android:layout_margin="5dp"
            android:textSize="25dp"
            android:background="@drawable/shape_ceng"/>

    </LinearLayout>

</LinearLayout>

2、编码

(1)控件初始化

熟悉我的编码风格的,不用看都知道是绑定控件信息

	Button zero,one,two,there,four,five,six,seven,event,nine;
    Button jia,jiang,cheng,chu;
    Button dian,dengyu,ac;
    TextView text;

    private void init() {
        zero = findViewById(R.id.zero);
        one = findViewById(R.id.one);
        two = findViewById(R.id.two);
        there = findViewById(R.id.there);
        four = findViewById(R.id.four);
        five = findViewById(R.id.five);
        six = findViewById(R.id.six);
        seven = findViewById(R.id.seven);
        event = findViewById(R.id.event);
        nine = findViewById(R.id.nine);

        jia = findViewById(R.id.jia);
        jiang = findViewById(R.id.jiang);
        cheng = findViewById(R.id.cheng);
        chu = findViewById(R.id.chu);

        dian = findViewById(R.id.dian);
        dengyu = findViewById(R.id.dengyu);

        text = findViewById(R.id.jsq_text);
        ac = findViewById(R.id.ac);

        zero.setOnClickListener(clickListener);
        one.setOnClickListener(clickListener);
        two.setOnClickListener(clickListener);
        there.setOnClickListener(clickListener);
        four.setOnClickListener(clickListener);
        five.setOnClickListener(clickListener);
        six.setOnClickListener(clickListener);
        seven.setOnClickListener(clickListener);
        event.setOnClickListener(clickListener);
        zero.setOnClickListener(clickListener);
        nine.setOnClickListener(clickListener);
        jia.setOnClickListener(clickListener);
        jiang.setOnClickListener(clickListener);
        cheng.setOnClickListener(clickListener);
        chu.setOnClickListener(clickListener);
        dian.setOnClickListener(clickListener);
        dengyu.setOnClickListener(clickListener);
        ac.setOnClickListener(clickListener);
    }

(2)事件监听器

主要负责数据渲染和算法引用

    public View.OnClickListener clickListener = new View.OnClickListener() {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.zero:
                    js += "0";
                    text.setText(js);
                    break;
                case R.id.one:
                    js += "1";
                    text.setText(js);
                    break;
                case R.id.two:
                    js += "2";
                    text.setText(js);
                    break;
                case R.id.there:
                    js += "3";
                    text.setText(js);
                    break;
                case R.id.four:
                    js += "4";
                    text.setText(js);
                    break;
                case R.id.five:
                    js += "5";
                    text.setText(js);
                    break;
                case R.id.six:
                    js += "6";
                    text.setText(js);
                    break;
                case R.id.seven:
                    js += "7";
                    text.setText(js);
                    break;
                case R.id.event:
                    js += "8";
                    text.setText(js);
                    break;
                case R.id.nine:
                    js += "9";
                    text.setText(js);
                    break;
                case R.id.jia:
                    js += "+";
                    text.setText(js);
                    break;
                case R.id.jiang:
                    js += "-";
                    text.setText(js);
                    break;
                case R.id.cheng:
                    js += "*";
                    text.setText(js);
                    break;
                case R.id.chu:
                    js += "/";
                    text.setText(js);
                    break;
                case R.id.dian:
                    js += ".";
                    text.setText(js);
                    break;
                case R.id.dengyu:
                    double a = alg.parse(js);
                    text.setText(String.valueOf(a));
                    break;
                case R.id.ac:
                    js = "";
                    text.setText(js);
                    break;
            }
        }
    };

四、附件

源代码:https://download.csdn.net/download/weixin_48916759/87935078

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/668149.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【深度学习】4-1 误差反向传播法 - 计算图链式法则反向传播

上一章中神经网络的学习是通过数值微分计算的神经网络的权重参数的梯度。数值微分虽然简单&#xff0c;也容易实现&#xff0c;但缺点是计算上比较费时间。本章将学习一个能够高效计算权重参数的梯度的方法一一误差反向传播法。 误差反向传播法能够高效计算权重参数的梯度的方法…

Angular 安装与创建第一项目

1. 下载nodejs 并且安装 https://nodejs.org/en 2. 打开命令窗口&#xff0c;验证是否安装成功 C:\Users\Harry>node -v v18.16.0C:\Users\Harry>npm -v 9.5.1 3. 安装Angular CLI C:\Users\Harry>npm install -g angular/cliadded 239 packages in 9s npm notic…

Python Anaconda创建虚拟环境及Pycharm使用虚拟环境

目录 前言 一、Anaconda与Pycharm 二、conda常用命令 三、Pycharm使用虚拟环境 总结 前言 我们在做开发任务时可能会创建多个项目&#xff0c;这些项目可能会依赖于不同的Python环境。比如有的用到Python3.6、有的用到Python3.7&#xff1b;有的用Pytorch开发、有的用Tens…

SpringBoot整合模板引擎Thymeleaf(4)

版权声明 本文原创作者&#xff1a;谷哥的小弟作者博客地址&#xff1a;http://blog.csdn.net/lfdfhl 概述 在之前的教程中&#xff0c;我们介绍了Thymeleaf的基础知识。在此&#xff0c;以案例形式详细介绍Thymeleaf的基本使用。 项目结构 要点概述&#xff1a; 1、在st…

性能优化往往成为 Android 高工的一道分水岭

不论是大厂小厂&#xff0c;对于Android开发者来说&#xff0c;性能优化往往成为了是否真正配得上高级开发的一道分水岭&#xff0c;性能优化也是如今大厂在招聘要求中作出要求&#xff0c;且会高频提问&#xff1a; Android的性能优化&#xff0c;主要从以下几个方面开展&…

论文解读|基于RealSense的三维散乱部件点云分割

原创 | 文 BFT机器人 01 摘要 本文提出了一种针对垃圾拾取系统中点云分割的算法。该算法使用低成本的深度相机RealSense获取点云数据&#xff0c;并对点云数据进行滤波处理和分割&#xff0c;最终将分割后的子块片段独立地连接起来&#xff0c;形成完整的工件模型。通过测试案…

spark 数据倾斜处理

spark优化总结: 一、spark 代码优 六大代码优化: 避免创建重复的RDD 尽可能复用同一个RDD 对多次使用的RDD进行持久化 尽量避免使用shuffle类算子 使用map-side预聚合的shuffle操作 使用高性能的算子 广播大变量 使用Kryo优化序列化性能 优化数据结构 使用高性能的库fa…

怎么通过电商数据分析选择好货源?

什么样的货源才算好货源&#xff1f;自然是拿货成本低、销售前景&#xff08;趋势&#xff09;好、利润度高、去库存快的。这就需要综合销售、库存、财务、采购等多部门环节的数据进行分析挖掘&#xff0c;最终才能找到符合需求的供货商以及商品清单。在这个过程中&#xff0c;…

vue3引入uview-plus3.0移动组件库

vue3引入uview-plus3.0移动组件库 引入流程 导入插件到项目 项目地址&#xff1a;https://ext.dcloud.net.cn/plugin?nameuview-plus 在main.js引入uview // main.js import uviewPlus from /uni_modules/uview-plus// #ifdef VUE3 import { createSSRApp } from vue expor…

红日ATTCK系列靶场(-)简记

Step 1>》信息收集 nmap 发现80、 3306 nmap -T4 -O 192.168.92.100 访问80端口 dirsearch(御剑)扫描 发现&#xff1a;/phpMyadmin Step 2 》漏洞利用 1.弱口令 http://192.168.92.100/phpMyadmin root/root 登录成功 2.getshell select basedir //查绝对路径 int…

Flink 学习三 Flink 流 process function API

Flink 学习三 Flink 流&process function API 1.Flink 多流操作 1.1.split 分流 (deprecated) 把一个数据流根据数据分成多个数据流 1.2 版本后移除 1.2.分流操作 (使用侧流输出) public class _02_SplitStream {public static void main(String[] args) throws Excep…

Python接口自动化之登录接口测试

01 什么是接口&#xff1f; 接口&#xff1a;检测外部系统与系统之间以及内部各个子系统之间的交互点。 通俗来说&#xff0c;接口就是连接前后端的桥梁&#xff0c;接口测试可以简单理解为脱离了前端的功能测试。一个又一个的接口就对应功能测试内一个又一个的功能。但注意&am…

前端vue入门(纯代码)10

【10.TodoList-自定义事件】 TodoList案例的完整代码请点击此处粉色文字 TodoList案例中的子组件TodoHeader给父组件App传递数据 App.vue文件中需要修改的代码 原本&#xff1a; Todo案例中子给父传递数据【通信】的方法&#xff1a;props <!-- 把App组件里的方法addTodo(…

2023 Nature 健康系统规模的语言模型是通用预测引擎

文章目录 一、论文关键信息二、论文主要内容三、总结与讨论🍉 CSDN 叶庭云:https://yetingyun.blog.csdn.net/ 一、论文关键信息 论文标题:Health system-scale language models are all-purpose prediction engines 期刊信息:2023 Nature 论文地址:h

Armbian 23.05(代号Suni)操作系统已全面上市

Armbian社区通知我们&#xff0c;适用于ARM和RISC-V单板计算机以及其他平台的Armbian 23.05&#xff08;代号Suni&#xff09;操作系统已全面上市。 在Armbian 23.02发布三个月后&#xff0c;Armbian 23.05版本是第一个在完全重构的构建框架上创建的版本&#xff0c;基于即将发…

Flink 学习八 Flink 容错机制 checkpoint savepoint

Flink 学习八 Flink 容错机制 & checkpoint & savepoint https://nightlies.apache.org/flink/flink-docs-release-1.14/docs/concepts/stateful-stream-processing/ 1.容错基础概念 上一节讲述状态后端;Flink是一个 带状态stateful 的数据处理系统,在处理数据的过程…

基于深度学习的高精度蜜蜂检测识别系统(PyTorch+Pyside6+YOLOv5模型)

摘要&#xff1a;基于深度学习的高精度蜜蜂检测识别系统可用于日常生活中或野外来检测与定位蜜蜂目标&#xff0c;利用深度学习算法可实现图片、视频、摄像头等方式的蜜蜂目标检测识别&#xff0c;另外支持结果可视化与图片或视频检测结果的导出。本系统采用YOLOv5目标检测模型…

VUE 2X ClassStyle ⑦

目录 文章有误请指正&#xff0c;如果觉得对你有用&#xff0c;请点三连一波&#xff0c;蟹蟹支持✨ V u e j s Vuejs Vuejs C l a s s Class Class与 S t y l e Style Style绑定总结 文章有误请指正&#xff0c;如果觉得对你有用&#xff0c;请点三连一波&#xff0c;蟹蟹支持…

初始java String类型

文章目录 初始java String类型理解 next和nextLine的区别new String(); 括号里面可以放什么呢放byte类型的数组放byte类型的数组&#xff0c;索引&#xff0c;长度放char类型的数组放char类型的数组&#xff0c;索引&#xff0c;长度 String 类型对应同一字符串&#xff0c;是否…

boost 异步服务器开发

目录 1、 异步服务器简介 2、异步服务器开发 2.1 会话类 2.1.1 会话类头文件 2.1.2 会话类源文件 2.2 服务类 2.2.1 服务类头文件 2.2.2 服务类源文件 2.3 主函数 3、异步服务器测试 4、当前异步服务器存在的问题及后续优化 1、 异步服务器简介 boost 异步服务器分为…