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

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

相关推荐
努力努力再努力wz21 小时前
【Linux网络系列】:JSON+HTTP,用C++手搓一个web计算器服务器!
java·linux·运维·服务器·c语言·数据结构·c++
魂梦翩跹如雨21 小时前
死磕排序算法:手撕快速排序的四种姿势(Hoare、挖坑、前后指针 + 非递归)
java·数据结构·算法
2501_915921431 天前
iOS App 电耗管理 通过系统电池记录、Xcode Instruments 与克魔(KeyMob)组合使用
android·ios·小程序·https·uni-app·iphone·webview
带刺的坐椅1 天前
Solon AI Skills 会是 Agent 的未来吗?
java·agent·langchain4j·solon-ai
jacGJ1 天前
记录学习--文件读写
java·前端·学习
花间相见1 天前
【JAVA开发】—— Nginx服务器
java·开发语言·nginx
扶苏-su1 天前
Java---Properties 类
java·开发语言
cypking1 天前
四、CRUD操作指南
java
June bug1 天前
【配环境】安卓项目开发环境
android
2301_780669861 天前
文件字节流输出、文件复制、关闭流的方法
java