android计算器(按钮版)

 需求分析

 计算器的界面分为两大部分,第一部分是上方的计算表达式,既包括用户的按键输入,也包括计算结果数字;第二部分是下方的各个按键,例如:从0到9的数字按钮、加减乘除与等号、正负号按钮、小数点按钮、求倒数按钮、平方按钮、开方按钮,以及退格、清空、取消等控制按钮。

界面设计

简单计算器主要由上半部分的计算结果与下半部分的计算按钮两块区域组成,据此可山寨一个界面相似的计算器App,同样由计算结果和计算按钮两部分组成。

 简单计算器用到的控件

线性布局LinearLayout:计算器的整体布局是从上到下排列着的。

网格布局GridLayout:计算器下半部分的几排按钮,正好成五行四列表格分布,适合采用GridLayout。

滚动视图ScrollView:计算器界面如果超出屏幕大小,就要支持滚动。

文本视图TextView:计算结果文本需要使用TextView,且文字靠下靠右显示。

按钮Button:用于0-9的数字按键,以及加减乘除等运算按键。

关键代码

Toast控件,用于展示短暂的提示文字,

用法如下: Toast.makeText(MainActivity.this, "提示文字", Toast.LENGTH_SHORT).show(); 用户在计算器界面每输入一个按键,App都要进行下列两项操作:

(1)输入按键的合法性校验

(2)执行运算并显示计算结果

package com.example.chapter03;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class CalculateActivity extends AppCompatActivity implements View.OnClickListener {
    private final static String TAG = "CalculatorActivity";
    private TextView tv_result; // 文本视图对象
    private String operator = ""; // 运算符
    private String firstNum = ""; // 第一个操作数
    private String secondNum = ""; // 第二个操作数
    private String result = ""; // 当前的计算结果
    private String showText = ""; // 显示的文本内容

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);
        // 从布局文件中获取名叫tv_result的文本视图
        tv_result = findViewById(R.id.tv_result);
        // 下面给每个按钮控件都注册了点击监听器
        findViewById(R.id.btn_cancel).setOnClickListener(this); // “取消”按钮
        findViewById(R.id.btn_divide).setOnClickListener(this); // “除法”按钮
        findViewById(R.id.btn_multiply).setOnClickListener(this); // “乘法”按钮
        findViewById(R.id.btn_clear).setOnClickListener(this); // “清除”按钮
        findViewById(R.id.btn_seven).setOnClickListener(this); // 数字7
        findViewById(R.id.btn_eight).setOnClickListener(this); // 数字8
        findViewById(R.id.btn_nine).setOnClickListener(this); // 数字9
        findViewById(R.id.btn_plus).setOnClickListener(this); // “加法”按钮
        findViewById(R.id.btn_four).setOnClickListener(this); // 数字4
        findViewById(R.id.btn_five).setOnClickListener(this); // 数字5
        findViewById(R.id.btn_six).setOnClickListener(this); // 数字6
        findViewById(R.id.btn_minus).setOnClickListener(this); // “减法”按钮
        findViewById(R.id.btn_one).setOnClickListener(this); // 数字1
        findViewById(R.id.btn_two).setOnClickListener(this); // 数字2
        findViewById(R.id.btn_three).setOnClickListener(this); // 数字3
        findViewById(R.id.btn_reciprocal).setOnClickListener(this); // 求倒数按钮
        findViewById(R.id.btn_zero).setOnClickListener(this); // 数字0
        findViewById(R.id.btn_dot).setOnClickListener(this); // “小数点”按钮
        findViewById(R.id.btn_equal).setOnClickListener(this); // “等号”按钮
        findViewById(R.id.btn_genhao).setOnClickListener(this); // “开平方”按钮
    }

    private boolean verify(View v) {
        if (v.getId() == R.id.btn_cancel) { // 点击了取消按钮
            if (operator.equals("") && (firstNum.equals("") || firstNum.equals("0"))) { // 无运算符,则表示逐位取消第一个操作数
                Toast.makeText(this, "没有可取消的数字了", Toast.LENGTH_SHORT).show();
                return false;
            }
            if (!operator.equals("") && secondNum.equals("")) { // 有运算符,则表示逐位取消第二个操作数
                Toast.makeText(this, "没有可取消的数字了", Toast.LENGTH_SHORT).show();
                return false;
            }
        } else if (v.getId() == R.id.btn_equal) { // 点击了等号按钮
            if (operator.equals("")) { // 无运算符
                Toast.makeText(this, "请输入运算符", Toast.LENGTH_SHORT).show();
                return false;
            }
            if (firstNum.equals("") || secondNum.equals("")) { // 无操作数
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                return false;
            }
            if (operator.equals("÷") && Double.parseDouble(secondNum) == 0) { // 被除数为零
                Toast.makeText(this, "被除数不能为零", Toast.LENGTH_SHORT).show();
                return false;
            }
        } else if (v.getId() == R.id.btn_plus || v.getId() == R.id.btn_minus // 点击了加、减、乘、除按钮
                || v.getId() == R.id.btn_multiply || v.getId() == R.id.btn_divide) {
            if (firstNum.equals("")) { // 缺少第一个操作数
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                return false;
            }
            if (!operator.equals("")) { // 已有运算符
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                return false;
            }
        } else if (v.getId() == R.id.btn_genhao) { // 点击了开根号按钮
            if (firstNum.equals("")) { // 缺少底数
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                return false;
            }
            if (Double.parseDouble(firstNum) < 0) { // 不能对负数开平方
                Toast.makeText(this, "开根号的数值不能小于零", Toast.LENGTH_SHORT).show();
                return false;
            }
        } else if (v.getId() == R.id.btn_reciprocal) { // 点击了求倒数按钮
            if (firstNum.equals("")) { // 缺少底数
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                return false;
            }
            if (Double.parseDouble(firstNum) == 0) { // 不能对零求倒数
                Toast.makeText(this, "不能对零求倒数", Toast.LENGTH_SHORT).show();
                return false;
            }
        } else if (v.getId() == R.id.btn_dot) { // 点击了小数点
            if (operator.equals("") && firstNum.contains(".")) { // 无运算符,则检查第一个操作数是否已有小数点
                Toast.makeText(this, "一个数字不能有两个小数点", Toast.LENGTH_SHORT).show();
                return false;
            }
            if (!operator.equals("") && secondNum.contains(".")) { // 有运算符,则检查第二个操作数是否已有小数点
                Toast.makeText(this, "一个数字不能有两个小数点", Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        if (!verify(v)) { // 未通过合法性校验,直接返回
            return;
        }
        String inputText;
        if (v.getId() == R.id.btn_genhao) { // 如果是开根号按钮
            inputText = "√";
        } else { // 除了开根号之外的其他按钮
            inputText = ((TextView) v).getText().toString();
        }
        Log.d(TAG, "inputText=" + inputText);
        if (v.getId() == R.id.btn_clear) { // 点击了清除按钮
            clear();
        } else if (v.getId() == R.id.btn_cancel) { // 点击了取消按钮
            if (operator.equals("")) { // 无运算符,则表示逐位取消第一个操作数
                if (firstNum.length() == 1) {
                    firstNum = "0";
                } else if (firstNum.length() > 1) {
                    firstNum = firstNum.substring(0, firstNum.length() - 1);
                }
                refreshText(firstNum);
            } else { // 有运算符,则表示逐位取消第二个操作数
                if (secondNum.length() == 1) {
                    secondNum = "";
                } else if (secondNum.length() > 1) {
                    secondNum = secondNum.substring(0, secondNum.length() - 1);
                }
                refreshText(showText.substring(0, showText.length() - 1));
            }
        } else if (v.getId() == R.id.btn_plus || v.getId() == R.id.btn_minus // 点击了加、减、乘、除按钮
                || v.getId() == R.id.btn_multiply || v.getId() == R.id.btn_divide) {
            operator = inputText; // 运算符
            refreshText(showText + operator);
        } else if (v.getId() == R.id.btn_equal) { // 点击了等号按钮
            double caculate_result = caculateFour(); // 加减乘除四则运算
            refreshOperate(String.valueOf(caculate_result));
            refreshText(showText + "=" + result);
        } else if (v.getId() == R.id.btn_genhao ) { // 点击了开根号按钮
            double caculate_result = Math.sqrt(Double.parseDouble(firstNum)); // 开平方运算
            refreshOperate(String.valueOf(caculate_result));
            refreshText(showText + "√=" + result);
        } else if (v.getId() == R.id.btn_reciprocal) { // 点击了求倒数按钮
            double caculate_result = 1.0 / Double.parseDouble(firstNum); // 求倒数运算
            refreshOperate(String.valueOf(caculate_result));
            refreshText(showText + "/=" + result);
        } else { // 点击了其他按钮,包括数字和小数点
            if (result.length() > 0 && operator.equals("")) { // 上次的运算结果已经出来了
                clear();
            }
            if (operator.equals("")) { // 无运算符,则继续拼接第一个操作数
                firstNum = firstNum+inputText;
            } else { // 有运算符,则继续拼接第二个操作数
                secondNum = secondNum + inputText;
            }
            if (showText.equals("0") && !inputText.equals(".")) { // 整数不需要前面的0
                refreshText(inputText);
            } else {
                refreshText(showText + inputText);
            }
        }
    }

    // 刷新运算结果
    private void refreshOperate(String new_result) {
        result = new_result;
        firstNum = result;
        secondNum = "";
        operator = "";
    }

    // 刷新文本显示
    private void refreshText(String text) {
        showText = text;
        tv_result.setText(showText);
    }

    // 清空并初始化
    private void clear() {
        refreshOperate("");
        refreshText("");
    }

    // 加减乘除四则运算,返回计算结果
    private double caculateFour() {
        double caculate_result = 0;
        if (operator.equals("+")) { // 当前是相加运算
            caculate_result = Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
        } else if (operator.equals("-")) { // 当前是相减运算
            caculate_result = Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
        } else if (operator.equals("×")) { // 当前是相乘运算
            caculate_result = Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
        } else if (operator.equals("÷")) { // 当前是相除运算
            caculate_result = Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
        }
        Log.d(TAG, "caculate_result=" + caculate_result); // 把运算结果打印到日志中
        return caculate_result;
    }

}
<?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="match_parent"
    android:orientation="vertical"
    android:background="@drawable/ars1"
    android:padding="5dp">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/simple_calculate"
            android:gravity="center"
            android:textColor="	#FFB6C1"
            android:textSize="25dp"
            android:background="@color/design_default_color_surface"
            android:textFontWeight="@integer/material_motion_duration_long_2"/>

        <TextView
            android:id="@+id/tv_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/touming"
            android:lines="3"
            android:text="0"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:textSize="40dp"
            android:gravity="right|bottom"
            />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnCount="4"
            android:rowCount="5">

            <Button
                android:id="@+id/btn_cancel"
                android:background="@color/touming"
                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/cancel"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/btn_divide"
                android:background="@color/touming"
                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/divide"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_multiply"
                android:background="@color/touming"
                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/multiply"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_clear"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/clear"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_seven"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/seven"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_eight"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/eight"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_nine"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/nine"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_plus"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/plus"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_four"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/four"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_five"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/five"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_six"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/six"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_minus"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/minus"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_one"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/one"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_two"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/two"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_three"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/three"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_genhao"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/genhao"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_reciprocal"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/reciprocal"
                android:textColor="@color/MidnightBlue"
                android:textSize="30dp"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_zero"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/zero"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_dot"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/dot"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/btn_equal"
                android:background="@color/touming"                android:layout_width="0dp"
                android:layout_height="@dimen/button_height"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:text="@string/equal"
                android:textColor="@color/MidnightBlue"
                android:textSize="@dimen/button_font_size"
                android:textStyle="bold"/>




        </GridLayout>
</ScrollView>




</LinearLayout>
<resources>
    <string name="app_name">chapter03</string>
    <string name="hello">你好,世界</string>
    <string name="simple_calculate">简易计算器</string>
    <string name="cancel">CE</string>
    <string name="divide">÷</string>
    <string name="multiply">×</string>
    <string name="clear">C</string>
    <string name="minus">-</string>
    <string name="plus">+</string>
    <string name="dot">.</string>
    <string name="equal">=</string>
    <string name="reciprocal">1/x</string>
    <string name="genhao">√ ̄</string>
    <string name="one">1</string>
    <string name="two">2</string>
    <string name="three">3</string>
    <string name="four">4</string>
    <string name="five">5</string>
    <string name="six">6</string>
    <string name="seven">7</string>
    <string name="eight">8</string>
    <string name="nine">9</string>
    <string name="zero">0</string>

</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <dimen name="button_font_size">35sp</dimen>
    <dimen name="button_height">90dp</dimen>
</resources>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小郭同学忒骚了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值