基于Android Studio 实现简单计算器(含完整代码)

在本文中,我们将使用Android Studio开发工具,实现一个简单的计算器应用程序。这个应用程序将能够进行基本的加减乘除运算,并展示运算结果。

步骤一:创建新项目

  1. 打开Android Studio,点击“Create New Project”来创建一个新的Android项目。
  2. 在弹出的窗口中,输入应用程序名称(例如:SimpleCalculator),选择保存位置,然后点击“Next”。
  3. 在选择设备类型和最低SDK版本时,选择“Phone and Tablet”并选择一个合适的最低SDK版本,然后点击“Next”。
  4. 在“Add an Activity to Mobile”界面上,选择“Empty Activity”,然后点击“Next”。
  5. 在“Configure Activity”界面上,保持默认设置,点击“Finish”完成项目创建。

步骤二:设计用户界面

  1. 打开res/layout/activity_main.xml文件,使用XML布局设计计算器界面。以下是一个简单的示例:
<?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: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="@string/simple_calculator"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/tv_result"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:gravity="right|bottom"
                android:lines="3"
                android:text="0"
                android:textColor="@color/black"
                android:textSize="25sp" />

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

                <Button
                    android:id="@+id/btn_cancel"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/cancel"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_divide"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/divide"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_multiply"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/multiply"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_clear"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/clear"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_seven"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/seven"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_eight"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/eight"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_nine"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/nine"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_plus"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/plus"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_four"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/four"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_five"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/five"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_six"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/six"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_minus"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/minus"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_one"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/one"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_two"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/two"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_three"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/three"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_sqrt"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/sqrt"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

<!--                <ImageButton-->
<!--                    android:id="@+id/btn_sqrt"-->
<!--                    android:layout_width="0dp"-->
<!--                    android:layout_height="@dimen/button_height"-->
<!--                    android:layout_columnWeight="1"-->
<!--                    android:scaleType="centerInside"-->
<!--                    android:src="@drawable/ic_launcher_background"-->
<!--                    />-->

                <Button
                    android:id="@+id/btn_reciprocal"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/reciprocal"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_zero"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/zero"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_dot"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/dot"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_equal"
                    android:layout_columnWeight="1"
                    android:width="0dp"
                    android:height="@dimen/button_height"
                    android:gravity="center"
                    android:text="@string/equal"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

            </GridLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>
  1. 在res/values下创建一个用来定义尺寸的资源文件dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <dimen name="button_font_size">30sp</dimen>
    <dimen name="button_height">75dp</dimen>

</resources>
  1. 修改res/values下的字符串资源文件strings.xml
<resources>
    <string name="app_name">simpleCalculator</string>
    <string name="simple_calculator">简单计算器</string>
    <string name="cancel">CE</string>
    <string name="divide">/</string>
    <string name="multiply">x</string>
    <string name="clear">C</string>
    <string name="seven">7</string>
    <string name="eight">8</string>
    <string name="nine">9</string>
    <string name="plus">+</string>
    <string name="four">4</string>
    <string name="five">5</string>
    <string name="six">6</string>
    <string name="minus">-</string>
    <string name="one">1</string>
    <string name="two">2</string>
    <string name="three">3</string>
    <string name="sqrt"></string>
    <string name="reciprocal">1/x</string>
    <string name="zero">0</string>
    <string name="dot">.</string>
    <string name="equal">=</string>
</resources>

步骤三:实现逻辑代码

  1. 打开MainActivity.java文件,实现计算器的逻辑功能。以下是代码:
package com.example.simplecalculator;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_result;
    private String firstNum = "";
    private String operator = "";
    private String secondNum = "";
    private String result = "";
    private String showText = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        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);
        findViewById(R.id.btn_eight).setOnClickListener(this);
        findViewById(R.id.btn_nine).setOnClickListener(this);
        findViewById(R.id.btn_plus).setOnClickListener(this);

        findViewById(R.id.btn_four).setOnClickListener(this);
        findViewById(R.id.btn_five).setOnClickListener(this);
        findViewById(R.id.btn_six).setOnClickListener(this);
        findViewById(R.id.btn_minus).setOnClickListener(this);

        findViewById(R.id.btn_one).setOnClickListener(this);
        findViewById(R.id.btn_two).setOnClickListener(this);
        findViewById(R.id.btn_three).setOnClickListener(this);
        findViewById(R.id.btn_reciprocal).setOnClickListener(this);

        findViewById(R.id.btn_zero).setOnClickListener(this);
        findViewById(R.id.btn_dot).setOnClickListener(this);
        findViewById(R.id.btn_equal).setOnClickListener(this);
        findViewById(R.id.btn_sqrt).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String inputText;
        if (view.getId() == R.id.btn_sqrt) {
            inputText = "√";
        } else {
            inputText = ((TextView) view).getText().toString();
        }

        if (view.getId() == R.id.btn_clear) {
            clear();

        } else if (view.getId() == R.id.btn_cancel) {

        }

        //加减乘除
        else if (view.getId() == R.id.btn_plus) {//加
            operator = inputText;
            refreshText(showText + operator);
        } else if (view.getId() == R.id.btn_minus) {//减
            operator = inputText;
            refreshText(showText + operator);
        } else if (view.getId() == R.id.btn_multiply) {//乘
            operator = inputText;
            refreshText(showText + operator);
        } else if (view.getId() == R.id.btn_divide) {//除
            operator = inputText;
            refreshText(showText + operator);
        }

        //等于
        else if (view.getId() == R.id.btn_equal) {//等于
            double calculate_result = calculateFour();
            refreshOperate(String.valueOf(calculate_result));
            refreshText(showText + "=" + result);
//            if (!result.isEmpty() && operator.isEmpty()){
//                clear();
//            }
        }

        else if (view.getId() == R.id.btn_sqrt) {//开平方
            double sqrt_result = Math.sqrt(Double.parseDouble(firstNum));
            refreshOperate(String.valueOf(sqrt_result));
            refreshText(showText + "√=" + result);
        }

        else if (view.getId() == R.id.btn_reciprocal) {//倒数
            double reciprocal_result =1.0/Double.parseDouble(firstNum);
            refreshOperate(String.valueOf(reciprocal_result));
            refreshText(showText + "/=" + result);
        }

        else {//点击其他按钮 包括数字和小数点
            if (operator.equals("")) {
                //无运算符,则继续拼接第一个操作数
                firstNum = firstNum + inputText;
            } else {
                //有运算符,则拼接第二个操作数
                secondNum = secondNum + inputText;
            }
            //整数不需要拼接0
            if (showText.equals("0") && !inputText.equals(".")) {
                refreshText(inputText);
            } else {
                refreshText(showText + inputText);
            }
        }
    }

    //四则运算
    private double calculateFour() {
        switch (operator) {
            case "+":
                return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
            case "-":
                return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
            case "x":
                return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
            case "/":
                return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
        }
        return 0;
    }

    //清空文本框
    private void clear() {
        refreshOperate("");
        refreshText("");
    }

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

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

步骤四:运行和测试

  1. 连接你的Android设备或启动模拟器,确保Android Studio正确配置。接着,点击Android Studio中的运行按钮来安装和启动你的应用程序。
  2. 应用程序启动后,你将看到一个简单的计算器界面,包括数字按钮(0-9)、运算符按钮(+、-、*、/)、清除按钮(C)、小数点按钮(.)和等号按钮(=)。
  3. 输入数字和运算符来执行计算。例如,输入数字和运算符后,点击等号按钮进行计算,结果将显示在顶部文本框中。
  4. 点击清除按钮可以清除当前输入和计算结果,重新开始新的计算。
  5. 效果预览:
    在这里插入图片描述
  • 26
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值