写一个完善的android计算器代码

时间: 2024-03-12 10:44:30 浏览: 16
下面是一个完善的Android计算器代码,实现简单的加、减、乘、除功能,以及清除和删除功能: ```java import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView tvResult; private String currentNumber = ""; private String lastOperation = ""; private double result = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = findViewById(R.id.tvResult); findViewById(R.id.btn0).setOnClickListener(this); findViewById(R.id.btn1).setOnClickListener(this); findViewById(R.id.btn2).setOnClickListener(this); findViewById(R.id.btn3).setOnClickListener(this); findViewById(R.id.btn4).setOnClickListener(this); findViewById(R.id.btn5).setOnClickListener(this); findViewById(R.id.btn6).setOnClickListener(this); findViewById(R.id.btn7).setOnClickListener(this); findViewById(R.id.btn8).setOnClickListener(this); findViewById(R.id.btn9).setOnClickListener(this); findViewById(R.id.btnAdd).setOnClickListener(this); findViewById(R.id.btnSubtract).setOnClickListener(this); findViewById(R.id.btnMultiply).setOnClickListener(this); findViewById(R.id.btnDivide).setOnClickListener(this); findViewById(R.id.btnClear).setOnClickListener(this); findViewById(R.id.btnDelete).setOnClickListener(this); findViewById(R.id.btnEqual).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn0: currentNumber += "0"; tvResult.setText(currentNumber); break; case R.id.btn1: currentNumber += "1"; tvResult.setText(currentNumber); break; case R.id.btn2: currentNumber += "2"; tvResult.setText(currentNumber); break; case R.id.btn3: currentNumber += "3"; tvResult.setText(currentNumber); break; case R.id.btn4: currentNumber += "4"; tvResult.setText(currentNumber); break; case R.id.btn5: currentNumber += "5"; tvResult.setText(currentNumber); break; case R.id.btn6: currentNumber += "6"; tvResult.setText(currentNumber); break; case R.id.btn7: currentNumber += "7"; tvResult.setText(currentNumber); break; case R.id.btn8: currentNumber += "8"; tvResult.setText(currentNumber); break; case R.id.btn9: currentNumber += "9"; tvResult.setText(currentNumber); break; case R.id.btnAdd: performOperation(); lastOperation = "+"; break; case R.id.btnSubtract: performOperation(); lastOperation = "-"; break; case R.id.btnMultiply: performOperation(); lastOperation = "*"; break; case R.id.btnDivide: performOperation(); lastOperation = "/"; break; case R.id.btnClear: clear(); break; case R.id.btnDelete: delete(); break; case R.id.btnEqual: calculate(); break; } } private void performOperation() { if (!currentNumber.isEmpty()) { double number = Double.parseDouble(currentNumber); if (result == 0) { result = number; } else { switch (lastOperation) { case "+": result += number; break; case "-": result -= number; break; case "*": result *= number; break; case "/": result /= number; break; } } currentNumber = ""; } } private void calculate() { if (!currentNumber.isEmpty()) { performOperation(); } tvResult.setText(String.valueOf(result)); result = 0; lastOperation = ""; } private void clear() { currentNumber = ""; result = 0; lastOperation = ""; tvResult.setText(""); } private void delete() { if (!currentNumber.isEmpty()) { currentNumber = currentNumber.substring(0, currentNumber.length() - 1); tvResult.setText(currentNumber); } } } ``` 在xml中,我们可以使用如下代码布局: ```xml <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:padding="16dp" android:background="@color/colorPrimaryDark" android:textColor="@android:color/white" android:gravity="right"/> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:rowCount="5" android:columnCount="4" android:padding="8dp"> <Button android:id="@+id/btnClear" android:layout_width="0dp" android:layout_height="wrap_content" android:text="C" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btnDelete" android:layout_width="0dp" android:layout_height="wrap_content" android:text="DEL" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btnDivide" android:layout_width="0dp" android:layout_height="wrap_content" android:text="/" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btnMultiply" android:layout_width="0dp" android:layout_height="wrap_content" android:text="×" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btn7" android:layout_width="0dp" android:layout_height="wrap_content" android:text="7" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btn8" android:layout_width="0dp" android:layout_height="wrap_content" android:text="8" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btn9" android:layout_width="0dp" android:layout_height="wrap_content" android:text="9" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btnSubtract" android:layout_width="0dp" android:layout_height="wrap_content" android:text="-" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="wrap_content" android:text="4" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="wrap_content" android:text="5" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btn6" android:layout_width="0dp" android:layout_height="wrap_content" android:text="6" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btnAdd" android:layout_width="0dp" android:layout_height="wrap_content" android:text="+" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="1" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="2" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="wrap_content" android:text="3" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btnEqual" android:layout_width="0dp" android:layout_height="wrap_content" android:text="=" android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="2" android:layout_gravity="fill"/> <Button android:id="@+id/btn0" android:layout_width="0dp" android:layout_height="wrap_content" android:text="0" android:textSize="24sp" android:layout_columnWeight="2" android:layout_rowWeight="1" android:layout_gravity="fill"/> <Button android:id="@+id/btnDecimal" android:layout_width="0dp" android:layout_height="wrap_content" android:text="." android:textSize="24sp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill"/> </GridLayout> </LinearLayout> ``` 这里使用了GridLayout来实现按钮的布局,代码中使用了字符串转换成数字的方法实现计算功能。

相关推荐

最新推荐

recommend-type

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

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

Android实现简单加法计算器

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

Android开发实现的简单计算器功能【附完整demo源码下载】

主要介绍了Android开发实现的简单计算器功能,结合实例形式分析了Android计算器的具体实现步骤与相关操作技巧,并附带完整demo源码供读者下载参考,需要的朋友可以参考下
recommend-type

基于Android 7.0与Android Studio的安卓学习.zip

Android是一种基于Linux内核(不包含GNU组件)的自由及开放源代码的移动操作系统,主要应用于移动设备,如智能手机和平板电脑。该系统最初由安迪·鲁宾开发,后被Google公司收购并注资,随后与多家硬件制造商、软件开发商及电信营运商共同研发改良。 Android操作系统的特点包括: 开放源代码:Android系统采用开放源代码模式,允许开发者自由访问、修改和定制操作系统,这促进了技术的创新和发展,使得Android系统具有高度的灵活性和可定制性。 多任务处理:Android允许用户同时运行多个应用程序,并且可以轻松地在不同应用程序之间切换,提高了效率和便利性。 丰富的应用生态系统:Android系统拥有庞大的应用程序生态系统,用户可以从Google Play商店或其他第三方应用市场下载和安装各种各样的应用程序,满足各种需求。 可定制性:Android操作系统可以根据用户的个人喜好进行定制,用户可以更改主题、小部件和图标等,以使其界面更符合个人风格和偏好。 多种设备支持:Android操作系统可以运行在多种不同类型的设备上,包括手机、平板电脑、智能电视、汽车导航系统等。 此外,Android系统还有一些常见的问题,如应用崩溃、电池耗电过快、Wi-Fi连接问题、存储空间不足、更新问题等。针对这些问题,用户可以尝试一些基本的解决方法,如清除应用缓存和数据、降低屏幕亮度、关闭没有使用的连接和传感器、限制后台运行的应用、删除不需要的文件和应用等。 随着Android系统的不断发展,其功能和性能也在不断提升。例如,最新的Android版本引入了更多的安全性和隐私保护功能,以及更流畅的用户界面和更强大的性能。此外,Android系统也在不断探索新的应用场景,如智能家居、虚拟现实、人工智能等领域。 总之,Android系统是一种功能强大、灵活可定制、拥有丰富应用生态系统的移动操作系统,在全球范围内拥有广泛的用户基础。
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

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

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

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依