android实现一个简易多功能计算器代码和布局文件

时间: 2024-02-03 13:15:24 浏览: 30
以下是一个简单的 Android 计算器的布局文件和代码: layout/activity_main.xml ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:padding="16dp" android:gravity="right" android:textColor="@android:color/black" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_clear" android:text="C" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_divide" android:text="/" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_multiply" android:text="*" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_minus" android:text="-" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_7" android:text="7" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_8" android:text="8" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_9" android:text="9" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_plus" android:text="+" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_4" android:text="4" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_5" android:text="5" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_6" android:text="6" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_equals" android:text="=" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_1" android:text="1" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_2" android:text="2" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_3" android:text="3" android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_decimal" android:text="." android:textSize="24sp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/btn_0" android:text="0" android:textSize="24sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> ``` Java/MainActivity.java ```java public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView tvResult; private Button btnClear, btnDivide, btnMultiply, btnMinus, btnPlus, btnEquals, btnDecimal; private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9; private String currentNumber = ""; private String operation = ""; private double result = 0.0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = findViewById(R.id.tv_result); btnClear = findViewById(R.id.btn_clear); btnDivide = findViewById(R.id.btn_divide); btnMultiply = findViewById(R.id.btn_multiply); btnMinus = findViewById(R.id.btn_minus); btnPlus = findViewById(R.id.btn_plus); btnEquals = findViewById(R.id.btn_equals); btnDecimal = findViewById(R.id.btn_decimal); btn0 = findViewById(R.id.btn_0); btn1 = findViewById(R.id.btn_1); btn2 = findViewById(R.id.btn_2); btn3 = findViewById(R.id.btn_3); btn4 = findViewById(R.id.btn_4); btn5 = findViewById(R.id.btn_5); btn6 = findViewById(R.id.btn_6); btn7 = findViewById(R.id.btn_7); btn8 = findViewById(R.id.btn_8); btn9 = findViewById(R.id.btn_9); btnClear.setOnClickListener(this); btnDivide.setOnClickListener(this); btnMultiply.setOnClickListener(this); btnMinus.setOnClickListener(this); btnPlus.setOnClickListener(this); btnEquals.setOnClickListener(this); btnDecimal.setOnClickListener(this); 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); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_clear: currentNumber = ""; operation = ""; result = 0.0; tvResult.setText(""); break; case R.id.btn_divide: performOperation("/"); break; case R.id.btn_multiply: performOperation("*"); break; case R.id.btn_minus: performOperation("-"); break; case R.id.btn_plus: performOperation("+"); break; case R.id.btn_equals: calculateResult(); break; case R.id.btn_decimal: if (!currentNumber.contains(".")) { currentNumber += "."; } tvResult.setText(currentNumber); break; case R.id.btn_0: appendNumber("0"); break; case R.id.btn_1: appendNumber("1"); break; case R.id.btn_2: appendNumber("2"); break; case R.id.btn_3: appendNumber("3"); break; case R.id.btn_4: appendNumber("4"); break; case R.id.btn_5: appendNumber("5"); break; case R.id.btn_6: appendNumber("6"); break; case R.id.btn_7: appendNumber("7"); break; case R.id.btn_8: appendNumber("8"); break; case R.id.btn_9: appendNumber("9"); break; } } private void appendNumber(String number) { currentNumber += number; tvResult.setText(currentNumber); } private void performOperation(String op) { if (operation.isEmpty()) { result = Double.parseDouble(currentNumber); operation = op; currentNumber = ""; } else { calculateResult(); operation = op; } } private void calculateResult() { if (operation.isEmpty()) { return; } double secondNumber = Double.parseDouble(currentNumber); switch (operation) { case "+": result += secondNumber; break; case "-": result -= secondNumber; break; case "*": result *= secondNumber; break; case "/": result /= secondNumber; break; } currentNumber = ""; operation = ""; tvResult.setText(String.valueOf(result)); } } ```

相关推荐

最新推荐

recommend-type

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

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

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

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

Android实现简单加法计算器

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

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

主要介绍了Android中使用GridLayout网格布局来制作简单的计算器App的实例,GridLayout比表格布局TabelLayout更容易用来制作计算器这样的多按钮排列的界面,需要的朋友可以参考下
recommend-type

基于Java swing组件实现简易计算器

主要介绍了基于Java swing组件实现简易计算器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
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

云原生架构与soa架构区别?

云原生架构和SOA架构是两种不同的架构模式,主要有以下区别: 1. 设计理念不同: 云原生架构的设计理念是“设计为云”,注重应用程序的可移植性、可伸缩性、弹性和高可用性等特点。而SOA架构的设计理念是“面向服务”,注重实现业务逻辑的解耦和复用,提高系统的灵活性和可维护性。 2. 技术实现不同: 云原生架构的实现技术包括Docker、Kubernetes、Service Mesh等,注重容器化、自动化、微服务等技术。而SOA架构的实现技术包括Web Services、消息队列等,注重服务化、异步通信等技术。 3. 应用场景不同: 云原生架构适用于云计算环境下的应用场景,如容器化部署、微服务
recommend-type

JSBSim Reference Manual

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