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

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

相关推荐
快乐就好ya36 分钟前
Java多线程
java·开发语言
IT学长编程41 分钟前
计算机毕业设计 二手图书交易系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·二手图书交易系统
CS_GaoMing1 小时前
Centos7 JDK 多版本管理与 Maven 构建问题和注意!
java·开发语言·maven·centos7·java多版本
艾伦~耶格尔2 小时前
Spring Boot 三层架构开发模式入门
java·spring boot·后端·架构·三层架构
man20172 小时前
基于spring boot的篮球论坛系统
java·spring boot·后端
2401_858120532 小时前
Spring Boot框架下的大学生就业招聘平台
java·开发语言
S hh2 小时前
【Linux】进程地址空间
java·linux·运维·服务器·学习
小雨cc5566ru2 小时前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app
Java探秘者2 小时前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
攸攸太上2 小时前
Spring Gateway学习
java·后端·学习·spring·微服务·gateway