生成一个Android的计算器的代码

时间: 2024-05-07 09:18:42 浏览: 10
以下是一个简单的Android计算器应用程序的代码示例,使用Java语言和Android Studio进行开发: 1. activity_main.xml(应用程序的主布局文件) ``` <?xml version="1.0" encoding="utf-8"?> <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="16dp"> <TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:gravity="right" android:padding="8dp" android:text="0" android:textSize="24sp" /> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="4" android:rowCount="5"> <Button android:id="@+id/btn_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="1" android:textSize="24sp" /> <Button android:id="@+id/btn_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="2" android:textSize="24sp" /> <Button android:id="@+id/btn_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="3" android:textSize="24sp" /> <Button android:id="@+id/btn_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="+" android:textSize="24sp" /> <Button android:id="@+id/btn_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="4" android:textSize="24sp" /> <Button android:id="@+id/btn_5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="5" android:textSize="24sp" /> <Button android:id="@+id/btn_6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="6" android:textSize="24sp" /> <Button android:id="@+id/btn_subtract" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="-" android:textSize="24sp" /> <Button android:id="@+id/btn_7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="7" android:textSize="24sp" /> <Button android:id="@+id/btn_8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="8" android:textSize="24sp" /> <Button android:id="@+id/btn_9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="9" android:textSize="24sp" /> <Button android:id="@+id/btn_multiply" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="*" android:textSize="24sp" /> <Button android:id="@+id/btn_clear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="bottom" android:text="C" android:textSize="24sp" /> <Button android:id="@+id/btn_0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="0" android:textSize="24sp" /> <Button android:id="@+id/btn_equals" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="=" android:textSize="24sp" /> <Button android:id="@+id/btn_divide" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="/" android:textSize="24sp" /> </GridLayout> </LinearLayout> ``` 2. MainActivity.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 { private TextView tvResult; private String currentNumber; private String operator; private double result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = findViewById(R.id.tv_result); currentNumber = "0"; operator = ""; result = 0; Button btn1 = findViewById(R.id.btn_1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleNumberInput("1"); } }); Button btn2 = findViewById(R.id.btn_2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleNumberInput("2"); } }); Button btn3 = findViewById(R.id.btn_3); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleNumberInput("3"); } }); Button btn4 = findViewById(R.id.btn_4); btn4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleNumberInput("4"); } }); Button btn5 = findViewById(R.id.btn_5); btn5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleNumberInput("5"); } }); Button btn6 = findViewById(R.id.btn_6); btn6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleNumberInput("6"); } }); Button btn7 = findViewById(R.id.btn_7); btn7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleNumberInput("7"); } }); Button btn8 = findViewById(R.id.btn_8); btn8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleNumberInput("8"); } }); Button btn9 = findViewById(R.id.btn_9); btn9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleNumberInput("9"); } }); Button btn0 = findViewById(R.id.btn_0); btn0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleNumberInput("0"); } }); Button btnAdd = findViewById(R.id.btn_add); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleOperatorInput("+"); } }); Button btnSubtract = findViewById(R.id.btn_subtract); btnSubtract.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleOperatorInput("-"); } }); Button btnMultiply = findViewById(R.id.btn_multiply); btnMultiply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleOperatorInput("*"); } }); Button btnDivide = findViewById(R.id.btn_divide); btnDivide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleOperatorInput("/"); } }); Button btnClear = findViewById(R.id.btn_clear); btnClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { currentNumber = "0"; operator = ""; result = 0; tvResult.setText("0"); } }); Button btnEquals = findViewById(R.id.btn_equals); btnEquals.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { calculateResult(); } }); } private void handleNumberInput(String number) { if (currentNumber.equals("0")) { currentNumber = number; } else { currentNumber += number; } tvResult.setText(currentNumber); } private void handleOperatorInput(String op) { if (operator.isEmpty()) { result = Double.parseDouble(currentNumber); } else { calculateResult(); } operator = op; currentNumber = "0"; } private void calculateResult() { if (operator.isEmpty()) { return; } double currentNumberValue = Double.parseDouble(currentNumber); switch (operator) { case "+": result += currentNumberValue; break; case "-": result -= currentNumberValue; break; case "*": result *= currentNumberValue; break; case "/": if (currentNumberValue == 0) { tvResult.setText("Error"); return; } result /= currentNumberValue; break; } currentNumber = String.valueOf(result); operator = ""; tvResult.setText(currentNumber); } } ``` 这是一个简单的四则运算计算器应用程序,用户可以在应用程序中输入数字和运算符,并按下“=”按钮计算结果。

相关推荐

最新推荐

recommend-type

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

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

Android实现简单加法计算器

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

android开发计算器设计开发报告.doc

Android简单应用程序开发,简单计算器。 2程序说明: 1、计算器界面友好,方便使用。 2、具有基本的加、减、乘、除功能。 3、能够判断用户输入运算数是否正确。 4、支持小数运算。 5、具有清除功能,删除所有,c。
recommend-type

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

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

Android中使用GridLayout网格布局来制作简单的计算器App

主要介绍了Android中使用GridLayout网格布局来制作简单的计算器App的实例,GridLayout比表格布局TabelLayout更容易用来制作计算器这样的多按钮排列的界面,需要的朋友可以参考下
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用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

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