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

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

相关推荐
幽络源小助理几秒前
SpringBoot学生成绩管理系统设计与实现
java·spring boot·后端
树獭非懒11 分钟前
Android重学笔记|四大组件|Service由浅入深八连问
android·面试·客户端
IT技术图谱12 分钟前
【绝非标题党】jetpack之startUp组件原理解析
android·源码
RainbowSea15 分钟前
11. RabbitMQ 消息队列 Federation (Exchange 交换机和 Queue队列) + Shovel 同步的搭建配置
java·消息队列·rabbitmq
RainbowSea20 分钟前
7. MySQL 当中的 InnoDB 数据存储结构(详解)
java·sql·mysql
_一条咸鱼_24 分钟前
Android Hilt 框架之自定义绑定模块(四)
android
ChinaRainbowSea24 分钟前
9. RabbitMQ 消息队列幂等性,优先级队列,惰性队列的详细说明
java·javascript·分布式·后端·rabbitmq·ruby·java-rabbitmq
cccccchd30 分钟前
IDEA 中右侧没有显示Maven
java
写bug写bug1 小时前
图解六种常见负载均衡算法,一看就懂!
java·后端·负载均衡
追光的独行者1 小时前
Springboot框架—单元测试操作
java·spring boot·单元测试