用android stdio写一个计算器

时间: 2023-08-04 21:39:28 浏览: 48
好的,下面是一个简单的Android Studio计算器应用的实现。 1. 创建一个新的Android Studio项目,选择Empty Activity。 2. 打开activity_main.xml文件,在布局中添加一些按钮和TextView组件,如下所示: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/display" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="36sp" android:text="0" android:gravity="end" android:padding="16dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" android:textSize="24sp" /> <Button android:id="@+id/button_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" android:textSize="24sp" /> <Button android:id="@+id/button_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" android:textSize="24sp" /> <Button android:id="@+id/button_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="+" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" android:textSize="24sp" /> <Button android:id="@+id/button_5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" android:textSize="24sp" /> <Button android:id="@+id/button_6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" android:textSize="24sp" /> <Button android:id="@+id/button_subtract" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="-" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" android:textSize="24sp" /> <Button android:id="@+id/button_8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="8" android:textSize="24sp" /> <Button android:id="@+id/button_9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="9" android:textSize="24sp" /> <Button android:id="@+id/button_multiply" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="×" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_clear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="C" android:textSize="24sp" /> <Button android:id="@+id/button_0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" android:textSize="24sp" /> <Button android:id="@+id/button_equals" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="=" android:textSize="24sp" /> <Button android:id="@+id/button_divide" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="÷" android:textSize="24sp" /> </LinearLayout> </LinearLayout> ``` 3. 打开MainActivity.java文件,添加以下代码: ``` public class MainActivity extends AppCompatActivity { private TextView display; private String currentNumber = ""; private String operator = ""; private String result = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); display = findViewById(R.id.display); Button button0 = findViewById(R.id.button_0); Button button1 = findViewById(R.id.button_1); Button button2 = findViewById(R.id.button_2); Button button3 = findViewById(R.id.button_3); Button button4 = findViewById(R.id.button_4); Button button5 = findViewById(R.id.button_5); Button button6 = findViewById(R.id.button_6); Button button7 = findViewById(R.id.button_7); Button button8 = findViewById(R.id.button_8); Button button9 = findViewById(R.id.button_9); button0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNumber("0"); } }); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNumber("1"); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNumber("2"); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNumber("3"); } }); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNumber("4"); } }); button5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNumber("5"); } }); button6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNumber("6"); } }); button7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNumber("7"); } }); button8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNumber("8"); } }); button9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNumber("9"); } }); Button buttonAdd = findViewById(R.id.button_add); buttonAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setOperator("+"); } }); Button buttonSubtract = findViewById(R.id.button_subtract); buttonSubtract.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setOperator("-"); } }); Button buttonMultiply = findViewById(R.id.button_multiply); buttonMultiply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setOperator("×"); } }); Button buttonDivide = findViewById(R.id.button_divide); buttonDivide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setOperator("÷"); } }); Button buttonEquals = findViewById(R.id.button_equals); buttonEquals.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { calculate(); } }); Button buttonClear = findViewById(R.id.button_clear); buttonClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clear(); } }); } private void addNumber(String number) { currentNumber += number; display.setText(currentNumber); } private void setOperator(String newOperator) { if (!currentNumber.equals("")) { operator = newOperator; result = currentNumber; currentNumber = ""; } } private void calculate() { if (!currentNumber.equals("") && !operator.equals("")) { double number1 = Double.parseDouble(result); double number2 = Double.parseDouble(currentNumber); double resultValue = 0; switch (operator) { case "+": resultValue = number1 + number2; break; case "-": resultValue = number1 - number2; break; case "×": resultValue = number1 * number2; break; case "÷": resultValue = number1 / number2; break; } currentNumber = String.valueOf(resultValue); operator = ""; result = ""; display.setText(currentNumber); } } private void clear() { operator = ""; result = ""; currentNumber = ""; display.setText("0"); } } ``` 4. 运行应用程序,在计算器上点击数字和运算符,即可进行简单的计算。 注意:这只是一个简单的计算器应用程序,它并没有实现完整的计算器功能。如果您需要更复杂的计算器功能,请进行必要的更改和添加。

相关推荐

最新推荐

recommend-type

YOLOv8中加入CBAM注意力机制

YOLOv8中加入CBAM注意力机制,适合目标检测方向新手小白对YOLOv8作出改进,开箱即用,上传不易,小伙伴拿走的同时请顺手一键三连哈
recommend-type

高分项目 基于STM32单片机的语音导盲系统设计源代码+原理图+项目资料齐全+教程文档.zip

【资源概览】 高分项目 基于STM32单片机的语音导盲系统设计源代码+原理图+项目资料齐全+教程文档.zip高分项目 基于STM32单片机的语音导盲系统设计源代码+原理图+项目资料齐全+教程文档.zip高分项目 基于STM32单片机的语音导盲系统设计源代码+原理图+项目资料齐全+教程文档.zip 【资源说明】 高分项目源码:此资源是在校高分项目的完整源代码,经过导师的悉心指导与认可,答辩评审得分高达95分,项目的质量与深度有保障。 测试运行成功:所有的项目代码在上传前都经过了严格的测试,确保在功能上完全符合预期,您可以放心下载并使用。 适用人群广泛:该项目不仅适合计算机相关专业(如电子信息、物联网、通信工程、自动化等)的在校学生和老师,还可以作为毕业设计、课程设计、作业或项目初期立项的演示材料。对于希望进阶学习的小白来说,同样是一个极佳的学习资源。 代码灵活性高:如果您具备一定的编程基础,可以在此代码基础上进行个性化的修改,以实现更多功能。当然,直接用于毕业设计、课程设计或作业也是完全可行的。 欢迎下载,与我一起交流学习,共同进步!
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

如何用python编写api接口

在Python中编写API接口可以使用多种框架,其中比较流行的有Flask和Django。这里以Flask框架为例,简单介绍如何编写API接口。 1. 安装Flask框架 使用pip命令安装Flask框架: ``` pip install flask ``` 2. 编写API接口 创建一个Python文件,例如app.py,编写以下代码: ```python from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/hello', methods=['GET']) def hello():
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。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

matlab 将加载的数据转变为矩阵

在 MATLAB 中,可以使用 `load` 函数将数据加载到工作区中,然后将其转换为矩阵。 例如,假设我们有一个名为 `data.txt` 的文本文件,其中包含以下内容: ``` 1 2 3 4 5 6 7 8 9 ``` 我们可以使用以下代码将其加载并转换为矩阵: ``` data = load('data.txt'); matrix = reshape(data, [3, 3]); ``` `load` 函数将文件中的数据加载到名为 `data` 的变量中,该变量是一个向量。我们可以使用 `reshape` 函数将其转换为一个 3x3 的矩阵。