安卓——计算器应用(Java)

步骤 1: 设置Android Studio项目

创建一个新的Android项目,选择Java作为编程语言。

步骤 2: 设计用户界面

打开activity_main.xml文件,在res/layout目录下,设计你的计算器用户界面。这个例子使用了LinearLayout来排列两个EditText输入框和几个按钮。

XML 复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">

    <EditText
        android:id="@+id/number1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:hint="Enter number"/>

    <EditText
        android:id="@+id/number2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:hint="Enter number"/>

    <Button
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"/>

    <!-- Add buttons for Subtract, Multiply, and Divide -->

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="Result will be shown here"
        android:gravity="center"/>
</LinearLayout>

步骤 3: 实现计算逻辑

MainActivity.java文件中,添加逻辑来处理用户的输入和计算请求。这包括获取用户输入的数字,执行所请求的运算,然后显示结果。

XML 复制代码
package com.example.simplecalculator;

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

public class MainActivity extends AppCompatActivity {

    EditText number1, number2;
    TextView result;
    Button addButton, subtractButton, multiplyButton, divideButton;

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

        number1 = findViewById(R.id.number1);
        number2 = findViewById(R.id.number2);
        result = findViewById(R.id.result);
        addButton = findViewById(R.id.addButton);
        
        // Initialize other buttons

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                calculateResult("+");
            }
        });

        // Set onClickListener for Subtract, Multiply, and Divide buttons

    }

    private void calculateResult(String operation) {
        String num1 = number1.getText().toString();
        String num2 = number2.getText().toString();

        // Convert input to double
        double value1 = Double.parseDouble(num1);
        double value2 = Double.parseDouble(num2);
        double res = 0;

        switch (operation) {
            case "+":
                res = value1 + value2;
                break;
            // Handle other operations
        }

        result.setText(String.valueOf(res));
    }
}

在这个代码中,我们定义了EditText组件来接收用户的输入,TextView来显示结果,以及Button组件来执行加法操作。你需要扩展calculateResult方法和为减法、乘法、除法按钮设置OnClickListener来完成其他运算。

这个例子提供了一个基础框架,你可以根据需要扩展和优化它。例如,你可以添加输入验证来确保在执行运算之前,用户已经输入了有效的数字。

相关推荐
黄林晴11 分钟前
Nearby Connections 重大变更:不再自动开启 Wi-Fi / 蓝牙,适配指南
android
TDengine (老段)11 分钟前
TDengine 免费版说明
java·大数据·数据库·物联网·时序数据库·tdengine
码上有光15 分钟前
异常和智能指针
java·大数据·c++·servlet·异常·智能指针
恋猫de小郭31 分钟前
Flutter 全新真 3D 实现,用 flutter_scene 能开发一个「我的世界」
android·前端·flutter
学计算机的计算基1 小时前
操作系统内存管理全解:虚拟内存、页表、COW、malloc、OOM一篇搞定
java·笔记·算法
tachibana21 小时前
hot100 前 K 个高频元素(347)
java·数据结构·算法·leetcode
万亿少女的梦1681 小时前
基于Spring Boot、Java与MySQL的网络订餐系统设计与实现
java·spring boot·mysql·系统设计·网络订餐
咩咩啃树皮10 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
鱟鲥鳚10 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
大模型码小白12 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程