用最简洁的代码写出一个计算器android studio代码

时间: 2024-02-05 09:10:26 浏览: 21
以下是一个简单的计算器应用程序的 Android Studio 代码: MainActivity.java ``` import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnAdd, btnSubtract, btnMultiply, btnDivide, btnEquals, btnDecimal, btnClear; TextView display; String currentNumber; String operation; double result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn0 = findViewById(R.id.btn0); btn1 = findViewById(R.id.btn1); btn2 = findViewById(R.id.btn2); btn3 = findViewById(R.id.btn3); btn4 = findViewById(R.id.btn4); btn5 = findViewById(R.id.btn5); btn6 = findViewById(R.id.btn6); btn7 = findViewById(R.id.btn7); btn8 = findViewById(R.id.btn8); btn9 = findViewById(R.id.btn9); btnAdd = findViewById(R.id.btnAdd); btnSubtract = findViewById(R.id.btnSubtract); btnMultiply = findViewById(R.id.btnMultiply); btnDivide = findViewById(R.id.btnDivide); btnEquals = findViewById(R.id.btnEquals); btnDecimal = findViewById(R.id.btnDecimal); btnClear = findViewById(R.id.btnClear); display = findViewById(R.id.display); btn0.setOnClickListener(this); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); btn7.setOnClickListener(this); btn8.setOnClickListener(this); btn9.setOnClickListener(this); btnAdd.setOnClickListener(this); btnSubtract.setOnClickListener(this); btnMultiply.setOnClickListener(this); btnDivide.setOnClickListener(this); btnEquals.setOnClickListener(this); btnDecimal.setOnClickListener(this); btnClear.setOnClickListener(this); currentNumber = ""; operation = ""; result = 0; } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn0: currentNumber += "0"; display.setText(currentNumber); break; case R.id.btn1: currentNumber += "1"; display.setText(currentNumber); break; case R.id.btn2: currentNumber += "2"; display.setText(currentNumber); break; case R.id.btn3: currentNumber += "3"; display.setText(currentNumber); break; case R.id.btn4: currentNumber += "4"; display.setText(currentNumber); break; case R.id.btn5: currentNumber += "5"; display.setText(currentNumber); break; case R.id.btn6: currentNumber += "6"; display.setText(currentNumber); break; case R.id.btn7: currentNumber += "7"; display.setText(currentNumber); break; case R.id.btn8: currentNumber += "8"; display.setText(currentNumber); break; case R.id.btn9: currentNumber += "9"; display.setText(currentNumber); break; case R.id.btnAdd: calculate(); operation = "+"; break; case R.id.btnSubtract: calculate(); operation = "-"; break; case R.id.btnMultiply: calculate(); operation = "*"; break; case R.id.btnDivide: calculate(); operation = "/"; break; case R.id.btnEquals: calculate(); operation = ""; break; case R.id.btnDecimal: if (!currentNumber.contains(".")) { currentNumber += "."; } display.setText(currentNumber); break; case R.id.btnClear: currentNumber = ""; operation = ""; result = 0; display.setText(""); break; } } private void calculate() { if (currentNumber.length() > 0) { double number = Double.parseDouble(currentNumber); currentNumber = ""; if (operation.equals("+")) { result += number; } else if (operation.equals("-")) { result -= number; } else if (operation.equals("*")) { result *= number; } else if (operation.equals("/")) { result /= number; } else { result = number; } display.setText("" + result); } } } ``` activity_main.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/display" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:gravity="right" android:padding="16dp" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/btnClear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/display" android:layout_toLeftOf="@+id/btnDivide" android:layout_weight="1" android:text="C" /> <Button android:id="@+id/btnDivide" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/display" android:layout_toLeftOf="@+id/btnMultiply" android:layout_weight="1" android:text="/" /> <Button android:id="@+id/btnMultiply" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/display" android:layout_toLeftOf="@+id/btnSubtract" android:layout_weight="1" android:text="*" /> <Button android:id="@+id/btnSubtract" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/display" android:layout_toLeftOf="@+id/btnAdd" android:layout_weight="1" android:text="-" /> <Button android:id="@+id/btnAdd" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/display" android:layout_toLeftOf="@+id/btnEquals" android:layout_weight="1" android:text="+" /> <Button android:id="@+id/btnEquals" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/display" android:layout_alignParentRight="true" android:layout_weight="1" android:text="=" /> <Button android:id="@+id/btn7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btnClear" android:layout_toLeftOf="@+id/btn8" android:layout_weight="1" android:text="7" /> <Button android:id="@+id/btn8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btnClear" android:layout_toLeftOf="@+id/btn9" android:layout_weight="1" android:text="8" /> <Button android:id="@+id/btn9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btnClear" android:layout_toLeftOf="@+id/btnDivide" android:layout_weight="1" android:text="9" /> <Button android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btn7" android:layout_toLeftOf="@+id/btn5" android:layout_weight="1" android:text="4" /> <Button android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btn8" android:layout_toLeftOf="@+id/btn6" android:layout_weight="1" android:text="5" /> <Button android:id="@+id/btn6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btn9" android:layout_toLeftOf="@+id/btnMultiply" android:layout_weight="1" android:text="6" /> <Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btn4" android:layout_toLeftOf="@+id/btn2" android:layout_weight="1" android:text="1" /> <Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btn5" android:layout_toLeftOf="@+id/btn3" android:layout_weight="1" android:text="2" /> <Button android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btn6" android:layout_toLeftOf="@+id/btnSubtract" android:layout_weight="1" android:text="3" /> <Button android:id="@+id/btnDecimal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btn0" android:layout_toLeftOf="@+id/btnEquals" android:layout_weight="1" android:text="." /> <Button android:id="@+id/btn0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/btnClear" android:layout_toLeftOf="@+id/btnDecimal" android:layout_weight="1" android:text="0" /> </RelativeLayout> ```

相关推荐

最新推荐

recommend-type

Android Studio实现简易计算器(表格布局TableLayout)

主要为大家详细介绍了Android Studio实现简易计算器,表格布局TableLayout,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

FT-Prog-v3.12.38.643-FTD USB 工作模式设定及eprom读写

FT_Prog_v3.12.38.643--FTD USB 工作模式设定及eprom读写
recommend-type

matlab基于RRT和人工势场法混合算法的路径规划.zip

matlab基于RRT和人工势场法混合算法的路径规划.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。