Android Studio App开发实战项目之计算器的开发及演示(附源码和演示视频,超详细,可直接使用)

简介: Android Studio App开发实战项目之计算器的开发及演示(附源码和演示视频,超详细,可直接使用)

演示视频如下 需要源码请点赞关注收藏后评论区留言~~~

Android App开发计算器演示

计算器是人们日常生活中最常用的工具之一,无论在电脑上还是手机上,都少不了计算器的身影。

界面设计

界面由计算结果和计算按钮两部分组成   效果如下

根据计算器App的效果图 可以得出大致分布着下列Android控件

线性布局LinearLayout

网格布局GridLayout

滚动视图ScrollView

文本视图TextView

按钮Button

图像按钮ImageButton

关键部分

1:输入按键的合法性校验

App同用户交互的 过程中,时常要想用户反馈一些信息,例如点错了按钮,输入了非法字符和不合数学规则的运算等等 对于这些一句话的提示。Android设计了Toast控件,用于展示短暂的提示文字,Toast的用法很简单 只需以下一行代码

Toast.makeText(MainActivity.this,"提示文字",Toast.Length_SHORT).show();

上面代码用到了两个方法,分别是makeText和show,show方法用来展示提示窗,makeText方法用来构建提示文字的模板。

对于计算器来说,有好几种情况需要提示用户,比如除数不能为零,开根号的数值不能小于零,不能对零求导数等等。这时就能通过Toast控件弹窗提醒用户 效果如下

2:执行运算并显示计算结果

合法性校验通过,方能继续接下来的业务逻辑,倘若用户本次未输入与计算有关的按钮,则计算器只需拼接操作数或者运算符,倘若用户本次输入了与计算有关的按钮,则计算器立即执行运算操作并显示计算结果。 效果如下

部分源码如下

CalculatorActivity类代码如下

package com.example.chapter03;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class CalculatorActivity extends Avity 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_calculator);
        // 从布局文件中获取名叫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.ib_sqrt).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.ib_sqrt) { // 点击了开根号按钮
            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.ib_sqrt) { // 如果是开根号按钮
            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.ib_sqrt) { // 点击了开根号按钮
            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) - DfirstNum) * 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;
    }
}

activity_calculatorXML文件代码如下

android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eeeeee"
    android:orientation="vertical"
    android:padding="5dp">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="简单计算器"
                android:textColor="#000000"
                android:textSize="20sp" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/tv_result"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right|bottom"
                    android:lines="3"
                    android:maxLines="3"
                    android:scrollbars="vertical"
                    android:text="0"
                    android:textColor="#000000"
                    android:textSize="25sp" />
            </LinearLayout>
            <GridLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:columnCount="4">
                <Button
                    android:id="@+id/btn_cancel"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="CE"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_divide"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="÷"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_multiply"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="×"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_clear"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="C"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_seven"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="7"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_eight"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="8"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_nine"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="9"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_plus"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="+"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_four"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="4"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_five"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="5"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_six"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="6"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_minus"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="-"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_one"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="1"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_two"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="2"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_three"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="3"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <ImageButton
                    android:id="@+id/ib_sqrt"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:scaleType="centerInside"
                    android:src="@drawable/sqrt" />
                <Button
                    android:id="@+id/btn_reciprocal"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="1/x"
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_dot"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="."
                    android:textColor="@color/black"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_equal"
                    android:layout_width="90dp"
                    android:layout_height="75dp"
                    android:gravity="center"
                    android:text="="
                    android:textColor="@color/black"
                    android:textSize="30sp" />
            </GridLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>
相关文章
|
4天前
|
JSON Java API
Android 深入Http(5)从Retrofit源码来看Http,最新Android开发面试解答
Android 深入Http(5)从Retrofit源码来看Http,最新Android开发面试解答
|
4天前
|
API Android开发 C++
【字节跳动大牛系列教学】Android源码剖析之Framwork层消息传递
【字节跳动大牛系列教学】Android源码剖析之Framwork层消息传递
|
4天前
|
XML Android开发 数据格式
Fragment的使用,零基础入门android逆向视频课程
Fragment的使用,零基础入门android逆向视频课程
|
5天前
|
Android开发
在android源码中编译ADW_Launcher
在android源码中编译ADW_Launcher
12 2
|
5天前
|
Android开发
android studio 重新将module中的代码加入到自己项目中,报错找不到SO文件。
android studio 重新将module中的代码加入到自己项目中,报错找不到SO文件。
10 1
|
Android开发
【错误记录】Android Studio 编译报错 ( Installed Build Tools revision 31.0.0 is corrupted )
【错误记录】Android Studio 编译报错 ( Installed Build Tools revision 31.0.0 is corrupted )
691 0
【错误记录】Android Studio 编译报错 ( Installed Build Tools revision 31.0.0 is corrupted )
|
数据可视化 开发工具 Android开发
【错误记录】Android Studio 向 GitHub 提交代码报错 ( Push failed: Failed with error: Could not read | 使用命令行提交代码 )
【错误记录】Android Studio 向 GitHub 提交代码报错 ( Push failed: Failed with error: Could not read | 使用命令行提交代码 )
239 0
【错误记录】Android Studio 向 GitHub 提交代码报错 ( Push failed: Failed with error: Could not read | 使用命令行提交代码 )
|
Android开发
【错误记录】Android Studio 编译报错 ( Error:Connection timed out: connect | 更新配置依赖仓库方式 )
【错误记录】Android Studio 编译报错 ( Error:Connection timed out: connect | 更新配置依赖仓库方式 )
826 0
【错误记录】Android Studio 编译报错 ( Error:Connection timed out: connect | 更新配置依赖仓库方式 )
|
XML 缓存 IDE
解决Android Studio报错:Compilation is not supported for following modules
本文主要解决和"Compilation is not supported for following modules"有关的报错。
1480 0
解决Android Studio报错:Compilation is not supported for following modules
|
10月前
|
前端开发 Android开发
在android studio中启动模拟器的时候报错...keeps shopping
在android studio中启动模拟器的时候报错...keeps shopping
69 0
http://www.vxiaotou.com