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

这篇具有很好参考价值的文章主要介绍了【Android开发基础】计算器逻辑层代码补充。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、引言

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

二、设计

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;
	}

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

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样式是一样的

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

(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文章来源地址https://www.toymoban.com/news/detail-496409.html

到了这里,关于【Android开发基础】计算器逻辑层代码补充的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 移动应用开发实验一Android studio设计三种计算器的UI

    使用必要的布局方式,设计下面三种计算器的界面: 简单的计算器 科学计算器 程序计算器 边框的设置是建立一个drawable的xml文件,然后写了边框宽度、颜色、圆角和内边距。调用的时候用到了background属性 。

    2024年02月11日
    浏览(39)
  • android计算器界面布局线性布局跨2行,使用Kotlin高效地开发Android App(一,GitHub标星3.2K

    get(url).placeholder(R.drawable.shape_default_round_bg) .error(R.drawable.shape_default_round_bg) // .apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0))) .transform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0)) .into(this) } /** 占位符圆形 */ fun ImageView.loadCircle(url: Drawable) {

    2024年04月11日
    浏览(33)
  • Android计算器实现

    这个项目是一个简单的计算器应用,它可以执行加、减、乘、除四种基本运算等计算器的基本功能。我们将使用Android Studio作为开发工具。 1. 在Android Studio中创建新的Android项目。 2. 在布局文件(`activity_main.xml`)中,我们将添加一个按钮和一个用于显示结果的文本视图。 3. 在

    2024年02月07日
    浏览(30)
  • Android 实战项目:简单计算器

    虽然只学了一些Android的简单控件,但是只要活学善用这些布局和控件,也能够做出实用的App。 接下来让我们尝试设计并实现一个简单计算器。 Windows计算器,它主要由上半部分的计算结果与下半部分的计算按钮两块区域组成,据此可创建一个界面相似的计算器App,同样由计算

    2024年02月03日
    浏览(38)
  • 【Android Studio】简易计算器

    简易计算器要求: 1,操作简单,易于掌握,界面简单。 2.方便进行加,减,乘,除等操作。数字保留小数点后两位。 3.包含小数点运算和输入回退功能。 4.能够进行多次叠加运算。 5.系统能够进行多次叠加运算。 6.系统可以稳定运行。 功能图如下: 逻辑流程图如下: 项目建

    2024年02月08日
    浏览(32)
  • Android Studio简易计算器

    目录 第一步,创建新项目 第二步,设计UI 第三步,实现计算逻辑 第四步,测试应用程序 随着移动互联网的普及,手机应用程序已经成为人们生活中不可或缺的一部分。计算器是一类被广泛使用的应用程序之一,因此学习如何开发一款简易的计算器应用程序是学习Android Stu

    2024年02月08日
    浏览(26)
  • Android计算器设计实验报告

    一、实验目的:        熟悉Android稍微复杂的逻辑编程,应用网格布局设计一个简单Android计算器界面并能实现加减和清零等功能。 二、实验设备:        1.PC机        2.Windows操作系统        3.Android Studio 三、实验原理简述:        TableLayout是一种可以制作表格的布局

    2024年02月09日
    浏览(41)
  • 用javascript做一个计算器,用js做一个计算器代码

    大家好,给大家分享一下怎么用javascript做一个简单的计算器,很多人还不知道这一点。下面详细解释一下。现在让我们来看看! 页面布局设计(HTML+CSS)   由于在之前的博客中有对html和css进行详细的讲解,再次就不多叙述,直接上代码。因为js中用到了JQuery选择器所以在

    2024年02月04日
    浏览(54)
  • 【Android】控件与布局入门 - 简易计算器

    目录 1. 基础开发环境 2. 计算器的布局和相关按钮 3. 计算器的主要运算逻辑 4. APK 文件 5. 项目源码 JDK:JDK17 Android Studio:Android Studio Giraffe | 2022.3.1 Android SDK:Android API 34 Gradle: gradle-8.0-bin.zip 使用 LinearLayout 和 GridLayout 实现计算器的交互前端。 layout 文件如下 相关 values 如下:

    2024年02月14日
    浏览(38)
  • Java计算器简易代码

    我写的计算器 网上搜的进阶版本 拿走不谢!

    2024年02月11日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包