安卓——计算器应用(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来完成其他运算。

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

相关推荐
JAVA面经实录9177 分钟前
Java多线程并发高频面试100题(完整版·含答案·背诵版)
java·开发语言·面试
XiYang-DING11 分钟前
【Java EE】TCP—流量控制和拥塞控制
java·tcp/ip·java-ee
BIG_PEI30 分钟前
检查并安装Redis
java
大貔貅喝啤酒32 分钟前
基于Windows下载安装Android Studio 3.3.2版本教程(2026详细图文版)
android·java·windows·android studio
奋斗的小方34 分钟前
Java基础篇09:项目实战
java·开发语言
程序员码歌34 分钟前
OpenSpec 到 Superpowers:AI 编码从说清到做对
android·前端·人工智能
海兰35 分钟前
【第21篇-续】graph-Stream-Node改造为适配openAI模型示例
java·人工智能·spring boot·spring·spring ai
vKd0Ff21L37 分钟前
如何在Dev-C++中设置TDM-GCC为默认编译器第九十一篇
java·jvm·c++
武子康39 分钟前
Java-221 RocketMQ 消息存储核心原理:CommitLog、ConsumerQueue、IndexFile 与消息过滤机制
java·大数据·分布式·消息队列·rabbitmq·rocketmq·java-rocketmq
2501_9151063241 分钟前
深入解析无源码iOS加固原理与方案,保护应用安全
android·安全·ios·小程序·uni-app·cocoa·iphone