教你实现一个简单的 Android 计算器

作为一名新手开发者,制作一个简单的 Android 计算器可以帮助你掌握基本的 Android 开发知识。本文将为你提供一个详细的实现步骤,包括代码示例和解释。

实现流程

流程步骤概览

步骤 描述
1 创建新 Android 项目
2 设计用户界面
3 实现计算逻辑
4 测试和优化

步骤详解

步骤 1: 创建新 Android 项目

打开 Android Studio,选择 "新建项目",选择 "Empty Activity" 模板。填写项目的基本信息并点击 "Finish"。

步骤 2: 设计用户界面

res/layout/activity_main.xml 文件中添加简单的 Calculator 界面。

<!-- activity_main.xml -->
<RelativeLayout xmlns:android="
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:textSize="30sp"
        android:padding="20dp"/>

    <GridLayout
        android:id="@+id/buttonGrid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/display"
        android:rowCount="5"
        android:columnCount="4">

        <!-- 定义按钮 -->
        <Button android:text="7" style="@style/ButtonStyle" />
        <Button android:text="8" style="@style/ButtonStyle" />
        <Button android:text="9" style="@style/ButtonStyle" />
        <Button android:text="/" style="@style/ButtonStyle" />
        
        <Button android:text="4" style="@style/ButtonStyle" />
        <Button android:text="5" style="@style/ButtonStyle" />
        <Button android:text="6" style="@style/ButtonStyle" />
        <Button android:text="*" style="@style/ButtonStyle" />

        <Button android:text="1" style="@style/ButtonStyle" />
        <Button android:text="2" style="@style/ButtonStyle" />
        <Button android:text="3" style="@style/ButtonStyle" />
        <Button android:text="-" style="@style/ButtonStyle" />

        <Button android:text="0" style="@style/ButtonStyle" />
        <Button android:text="C" style="@style/ButtonStyle" />
        <Button android:text="=" style="@style/ButtonStyle" />
        <Button android:text="+" style="@style/ButtonStyle" />
        
    </GridLayout>
</RelativeLayout>

步骤 3: 实现计算逻辑

MainActivity.java 中实现计算器的逻辑,处理用户点击按钮。

// MainActivity.java
package com.example.calculator;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText display; // 声明显示区域
    private String operator = ""; // 记录运算符
    private float operand1 = 0; // 第一个操作数

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

        display = findViewById(R.id.display);

        // 按钮的点击事件
        findViewById(R.id.buttonGrid).setOnClickListener(view -> {
            Button button = (Button) view;
            onButtonClick(button);
        });
    }

    private void onButtonClick(Button button) {
        String buttonText = button.getText().toString();

        switch (buttonText) {
            case "C":
                clear();
                break;
            case "=":
                calculate();
                break;
            default:
                if ("/*-+".contains(buttonText)) {
                    operator = buttonText; // 设置运算符
                    operand1 = Float.parseFloat(display.getText().toString());
                    display.setText(""); // 清空显示区域
                } else {
                    display.append(buttonText); // 添加数字到显示区域
                }
                break;
        }
    }

    private void clear() {
        display.setText(""); // 清空显示区域
        operator = "";
        operand1 = 0;
    }

    private void calculate() {
        float operand2 = Float.parseFloat(display.getText().toString());
        float result = 0;

        // 根据运算符计算结果
        switch (operator) {
            case "+":
                result = operand1 + operand2;
                break;
            case "-":
                result = operand1 - operand2;
                break;
            case "*":
                result = operand1 * operand2;
                break;
            case "/":
                result = operand1 / operand2;
                break;
        }
        display.setText(String.valueOf(result)); // 显示计算结果
    }
}

步骤 4: 测试和优化

完成代码后,点击 "运行" 测试应用。确保所有的操作都正常。你可以对界面和算法进行更进一步的优化和美化。

关系图

以下是此计算器的ER图,展示了数据之间的关系。

erDiagram
    USERS {
        int id
        string name
    }
    CALCULATIONS {
        int id
        float operand1
        float operand2
        string operator
        float result
    }
    USERS ||--o{ CALCULATIONS : ""

类图

下面是计算器的类图。

classDiagram
    class MainActivity {
        - EditText display
        - String operator
        - float operand1
        + void onCreate(Bundle savedInstanceState)
        + void onButtonClick(Button button)
        + void calculate()
        + void clear()
    }

结语

本文介绍了如何实现一个简单的 Android 计算器,包括了从布局设计到实现逻辑的各个步骤。在实践中,可以尝试添加更多的功能,比如处理更复杂的运算,或者提升用户体验。希望这些内容能对你有帮助,祝你在 Android 开发的道路上越走越远!