Androidstudio生成一个计算器代码

时间: 2024-05-01 09:23:13 浏览: 16
以下是一个简单的 Android Studio 计算器应用程序的代码示例: 1. 创建一个新的 Android Studio 项目并设置基本信息(包名称、应用名称等)。 2. 打开 activity_main.xml 文件,并将以下代码添加到其中: ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/resultTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:background="@color/colorPrimaryDark" android:gravity="right" android:textColor="@android:color/white" android:textSize="32sp" android:textStyle="bold" /> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/resultTextView" android:columnCount="4" android:rowCount="5" android:orientation="horizontal"> <Button android:id="@+id/buttonClear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="C" android:textSize="24sp" /> <Button android:id="@+id/buttonBackspace" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="&lt;-" android:textSize="24sp" /> <Button android:id="@+id/buttonPercent" 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/buttonDivide" 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/buttonSeven" 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/buttonEight" 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/buttonNine" 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/buttonMultiply" 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/buttonFour" 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/buttonFive" 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/buttonSix" 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/buttonMinus" 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/buttonOne" 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/buttonTwo" 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/buttonThree" 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/buttonPlus" 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/buttonNegative" 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/buttonZero" 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/buttonDot" 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/buttonEquals" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="2" android:text="=" android:textSize="24sp" /> </GridLayout> </RelativeLayout> ``` 3. 打开 MainActivity.java 文件,并将以下代码添加到其中: ``` import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView resultTextView; private Button buttonClear, buttonBackspace, buttonPercent, buttonDivide, buttonSeven, buttonEight, buttonNine, buttonMultiply, buttonFour, buttonFive, buttonSix, buttonMinus, buttonOne, buttonTwo, buttonThree, buttonPlus, buttonNegative, buttonZero, buttonDot, buttonEquals; private String currentNumber = ""; private String operator = ""; private double result = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); resultTextView = findViewById(R.id.resultTextView); buttonClear = findViewById(R.id.buttonClear); buttonBackspace = findViewById(R.id.buttonBackspace); buttonPercent = findViewById(R.id.buttonPercent); buttonDivide = findViewById(R.id.buttonDivide); buttonSeven = findViewById(R.id.buttonSeven); buttonEight = findViewById(R.id.buttonEight); buttonNine = findViewById(R.id.buttonNine); buttonMultiply = findViewById(R.id.buttonMultiply); buttonFour = findViewById(R.id.buttonFour); buttonFive = findViewById(R.id.buttonFive); buttonSix = findViewById(R.id.buttonSix); buttonMinus = findViewById(R.id.buttonMinus); buttonOne = findViewById(R.id.buttonOne); buttonTwo = findViewById(R.id.buttonTwo); buttonThree = findViewById(R.id.buttonThree); buttonPlus = findViewById(R.id.buttonPlus); buttonNegative = findViewById(R.id.buttonNegative); buttonZero = findViewById(R.id.buttonZero); buttonDot = findViewById(R.id.buttonDot); buttonEquals = findViewById(R.id.buttonEquals); buttonClear.setOnClickListener(this); buttonBackspace.setOnClickListener(this); buttonPercent.setOnClickListener(this); buttonDivide.setOnClickListener(this); buttonSeven.setOnClickListener(this); buttonEight.setOnClickListener(this); buttonNine.setOnClickListener(this); buttonMultiply.setOnClickListener(this); buttonFour.setOnClickListener(this); buttonFive.setOnClickListener(this); buttonSix.setOnClickListener(this); buttonMinus.setOnClickListener(this); buttonOne.setOnClickListener(this); buttonTwo.setOnClickListener(this); buttonThree.setOnClickListener(this); buttonPlus.setOnClickListener(this); buttonNegative.setOnClickListener(this); buttonZero.setOnClickListener(this); buttonDot.setOnClickListener(this); buttonEquals.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.buttonClear: currentNumber = ""; operator = ""; result = 0; resultTextView.setText(""); break; case R.id.buttonBackspace: if (currentNumber.length() > 0) { currentNumber = currentNumber.substring(0, currentNumber.length() - 1); resultTextView.setText(currentNumber); } break; case R.id.buttonPercent: if (currentNumber.length() > 0) { double percentValue = Double.parseDouble(currentNumber) / 100; currentNumber = String.valueOf(percentValue); resultTextView.setText(currentNumber); } break; case R.id.buttonDivide: case R.id.buttonMultiply: case R.id.buttonMinus: case R.id.buttonPlus: if (operator.isEmpty()) { operator = ((Button) v).getText().toString(); result = Double.parseDouble(currentNumber); currentNumber = ""; } else { calculate(); operator = ((Button) v).getText().toString(); } break; case R.id.buttonEquals: calculate(); break; default: currentNumber += ((Button) v).getText().toString(); resultTextView.setText(currentNumber); break; } } private void calculate() { if (currentNumber.length() > 0) { double number = Double.parseDouble(currentNumber); switch (operator) { case "/": result /= number; break; case "*": result *= number; break; case "-": result -= number; break; case "+": result += number; break; } currentNumber = String.valueOf(result); resultTextView.setText(currentNumber); operator = ""; } } } ``` 4. 运行应用程序并测试它。现在你有了一个简单的 Android Studio 计算器应用程序!

相关推荐

最新推荐

recommend-type

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

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

解决Android Studio 代码自动提示突然失效的问题

主要介绍了解决Android Studio 代码自动提示突然失效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Android studio创建第一个app

主要为大家详细介绍了如何使用Android studio创建你的第一个项目Hello World,感兴趣的小伙伴们可以参考一下
recommend-type

解决Android Studio 代码无提示无颜色区分问题

主要介绍了解决Android Studio 代码无提示无颜色区分问题,需要的朋友可以参考下
recommend-type

android studio实现简单考试应用程序实例代码详解

主要介绍了android studio实现简单考试应用程序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
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://www.mathworks.com/discovery/image-segmentation/_jcr_content/mainParsys3/discoverysubsection_1185333930/mainParsys3/image_copy.adapt.full.medium.jpg/1712813808277.jpg) # 1. MATLAB开根号的理论基础 开根号运算在数学和科学计算中无处不在。在MATLAB中,开根号可以通过多种函数实现,包括`sqrt()`和`nthroot()`。`sqrt()`函数用于计算正实数的平方根,而`nt
recommend-type

react的函数组件的使用

React 的函数组件是一种简单的组件类型,用于定义无状态或者只读组件。 它们通常接受一个 props 对象作为参数并返回一个 React 元素。 函数组件的优点是代码简洁、易于测试和重用,并且它们使 React 应用程序的性能更加出色。 您可以使用函数组件来呈现简单的 UI 组件,例如按钮、菜单、标签或其他部件。 您还可以将它们与 React 中的其他组件类型(如类组件或 Hooks)结合使用,以实现更复杂的 UI 交互和功能。
recommend-type

JSBSim Reference Manual

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