安卓(Android studio)编写一个计算器,可以连续加减乘除,可加小数点,不包含括号以及加减乘除优先算法

时间: 2023-06-14 16:08:07 浏览: 73
好的,以下是一个简单的安卓计算器代码示例: 1. 在 XML 文件中创建布局: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/resultTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:textAlignment="center"/> <LinearLayout android:layout_below="@id/resultTextView" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="4"> <Button android:id="@+id/button1" android:text="1" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button2" android:text="2" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button3" android:text="3" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonAdd" android:text="+" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_below="@id/button1" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="4"> <Button android:id="@+id/button4" android:text="4" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button5" android:text="5" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button6" android:text="6" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonSubtract" android:text="-" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_below="@id/button4" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="4"> <Button android:id="@+id/button7" android:text="7" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button8" android:text="8" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button9" android:text="9" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonMultiply" android:text="*" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_below="@id/button7" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="4"> <Button android:id="@+id/buttonClear" android:text="C" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button0" android:text="0" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonDecimal" android:text="." android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonDivide" android:text="/" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/buttonEqual" android:text="=" android:layout_below="@id/buttonClear" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> ``` 2. 在 Java 文件中添加以下代码: ``` public class MainActivity extends AppCompatActivity { private TextView resultTextView; private String operand1 = ""; private String operand2 = ""; private String operator = ""; private String result = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); resultTextView = findViewById(R.id.resultTextView); // Set up number buttons findViewById(R.id.button0).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("0"); } }); findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("1"); } }); findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("2"); } }); findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("3"); } }); findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("4"); } }); findViewById(R.id.button5).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("5"); } }); findViewById(R.id.button6).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("6"); } }); findViewById(R.id.button7).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("7"); } }); findViewById(R.id.button8).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("8"); } }); findViewById(R.id.button9).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("9"); } }); findViewById(R.id.buttonDecimal).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberPressed("."); } }); // Set up operator buttons findViewById(R.id.buttonAdd).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { operatorPressed("+"); } }); findViewById(R.id.buttonSubtract).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { operatorPressed("-"); } }); findViewById(R.id.buttonMultiply).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { operatorPressed("*"); } }); findViewById(R.id.buttonDivide).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { operatorPressed("/"); } }); // Set up clear button findViewById(R.id.buttonClear).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clearPressed(); } }); // Set up equal button findViewById(R.id.buttonEqual).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { equalPressed(); } }); } private void numberPressed(String number) { if (operator.isEmpty()) { operand1 += number; resultTextView.setText(operand1); } else { operand2 += number; resultTextView.setText(operand2); } } private void operatorPressed(String operator) { this.operator = operator; resultTextView.setText(operator); } private void clearPressed() { operand1 = ""; operand2 = ""; operator = ""; result = ""; resultTextView.setText("0"); } private void equalPressed() { if (!operand1.isEmpty() && !operand2.isEmpty() && !operator.isEmpty()) { double num1 = Double.parseDouble(operand1); double num2 = Double.parseDouble(operand2); switch (operator) { case "+": result = String.valueOf(num1 + num2); break; case "-": result = String.valueOf(num1 - num2); break; case "*": result = String.valueOf(num1 * num2); break; case "/": if (num2 == 0) { result = "Error"; } else { result = String.valueOf(num1 / num2); } break; } resultTextView.setText(result); operand1 = result; operand2 = ""; operator = ""; result = ""; } } } ``` 这样就可以实现一个简单的安卓计算器了。

相关推荐

最新推荐

recommend-type

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

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

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

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
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

MATLAB数据可视化黑科技:洞悉数据背后的秘密

![MATLAB数据可视化黑科技:洞悉数据背后的秘密](https://img-blog.csdnimg.cn/img_convert/1a36558cefc0339f7836cca7680c0aef.png) # 1. MATLAB数据可视化的基本原理 MATLAB数据可视化是一种将数据转化为图形表示形式的技术,以帮助人们理解和分析数据。其基本原理包括: - **数据表示:**将原始数据转换为图形元素,如点、线和条形。 - **图形布局:**确定图形元素在绘图区域中的位置和大小。 - **图形美化:**添加标题、标签、颜色和纹理等元素,以增强图形的可读性和美观性。 - **交互性:**允
recommend-type

优化算法计算单位面积年平均输出热功率的代码

计算单位面积年平均输出热功率的代码具体情况取决于你所要解决的问题和需要的数据。然而,我可以给你一个通用的优化算法的框架,你可以根据具体情况进行调整。 首先,你需要明确计算单位面积年平均输出热功率的数学公式或算法。假设你已经有了计算热功率的函数 `calculate_power`,它接受一些输入参数,并返回热功率值。 下面是一个示例的优化算法框架,以最大化单位面积年平均输出热功率为目标: ```python import random def objective_function(params): # 这个函数用于计算目标函数的值 # 根据具体情况,你需要将 params