Android计算器:实现基本算术运算和科学运算

一、基本算术运算功能

计算器是我们日常生活中非常常见的工具之一,而作为一个开发者,能够开发出自己的计算器也是一种很具有成就感的事情。本文将介绍如何使用Android Studio开发一个基本算术运算的计算器。

在进行开发之前,我们需要了解计算器需要具备哪些基本的功能。首先,它需要支持常见的加、减、乘、除运算。我们可以使用Java语言中的计算方法来实现,例如:

int add(int a, int b) {//加法运算
   return a + b;
}

int substract(int a, int b) {//减法运算
   return a - b;
}

int multiply(int a, int b) {//乘法运算
   return a * b;
}

int divide(int a, int b) {//除法运算
   if (b == 0) {//除数不能为0
       throw new IllegalArgumentException("除数不能为0");
   }
   return a / b;
}

然后,我们需要在界面上添加数字按键和运算符按键,并且需要将它们的点击事件与计算器的输入和计算相关联。接下来,我们可以通过以下步骤来实现一个简单的计算器:

1. 创建一个Android项目,并在布局文件中添加一个EditText控件用于显示计算结果和输入数字,再添加多个Button控件用于输入数字和运算符。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">

   <EditText
       android:id="@+id/et_input"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="50dp"
       android:inputType="number"
       android:textAlignment="center"
       android:textSize="30sp"/>

   <Button
       android:id="@+id/button1"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="1"
       android:textSize="30sp"
       android:layout_below="@+id/et_input"
       android:layout_alignParentLeft="true"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button2"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="2"
       android:textSize="30sp"
       android:layout_below="@+id/et_input"
       android:layout_toRightOf="@+id/button1"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button3"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="3"
       android:textSize="30sp"
       android:layout_below="@+id/et_input"
       android:layout_toRightOf="@+id/button2"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button4"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="+"
       android:textSize="30sp"
       android:layout_below="@+id/et_input"
       android:layout_toRightOf="@+id/button3"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button5"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="4"
       android:textSize="30sp"
       android:layout_below="@+id/button1"
       android:layout_alignParentLeft="true"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button6"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="5"
       android:textSize="30sp"
       android:layout_below="@+id/button1"
       android:layout_toRightOf="@+id/button5"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button7"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="6"
       android:textSize="30sp"
       android:layout_below="@+id/button1"
       android:layout_toRightOf="@+id/button6"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button8"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="-"
       android:textSize="30sp"
       android:layout_below="@+id/button1"
       android:layout_toRightOf="@+id/button7"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button9"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="7"
       android:textSize="30sp"
       android:layout_below="@+id/button5"
       android:layout_alignParentLeft="true"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button10"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="8"
       android:textSize="30sp"
       android:layout_below="@+id/button5"
       android:layout_toRightOf="@+id/button9"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button11"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="9"
       android:textSize="30sp"
       android:layout_below="@+id/button5"
       android:layout_toRightOf="@+id/button10"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button12"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="*"
       android:textSize="30sp"
       android:layout_below="@+id/button5"
       android:layout_toRightOf="@+id/button11"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button13"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="."
       android:textSize="30sp"
       android:layout_below="@+id/button9"
       android:layout_alignParentLeft="true"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button14"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="0"
       android:textSize="30sp"
       android:layout_below="@+id/button9"
       android:layout_toRightOf="@+id/button13"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button15"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="="
       android:textSize="30sp"
       android:layout_below="@+id/button9"
       android:layout_toRightOf="@+id/button14"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

   <Button
       android:id="@+id/button16"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:text="/"
       android:textSize="30sp"
       android:layout_below="@+id/button9"
       android:layout_toRightOf="@+id/button15"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="20dp"/>

</RelativeLayout>

2. 在MainActivity类中定义一个变量用于保存当前输入的数字,定义一个变量用于保存当前输入的运算符,定义一个布尔型变量用于记录当前是否计算过表达式。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

   //输入的数字
   private String mText = "";

   //输入的运算符
   private String mOperator = "";

   //是否计算过表达式
   private boolean mIsCalculated = false;

   //...
}

3. 在MainActivity类的onCreate方法中找到布局中的所有数字和运算符按钮,并为它们设置点击事件。在点击事件中,根据用户输入的数字和运算符进行操作,最后将结果显示在EditText中。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

//...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
Button button10 = (Button) findViewById(R.id.button10);
Button button11 = (Button) findViewById(R.id.button11);
Button button12 = (Button) findViewById(R.id.button12);
Button button13 = (Button) findViewById(R.id.button13);
Button button14 = (Button) findViewById(R.id.button14);
Button button15 = (Button) findViewById(R.id.button15);
Button button16 = (Button) findViewById(R.id.button16);

button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
button10.setOnClickListener(this);
button11.setOnClickListener(this);
button12.setOnClickListener(this);
button13.setOnClickListener(this);
button14.setOnClickListener(this);
button15.setOnClickListener(this);
button16.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mIsCalculated) {
mText = "";
mOperator = "";
mIsCalculated = false;
}
switch (view.getId()) {
case R.id.button1:
clickNumber("1");
break;
case R.id.button2:
clickNumber("2");
break;
case R.id.button3:
clickNumber("3");
break;
case R.id.button5:
clickNumber("4");
break;
case R.id.button6:
clickNumber("5");
break;
case R.id.button7:
clickNumber("6");
break;
case R.id.button9:
clickNumber("7");
break;
case R.id.button10:
clickNumber("8");
break;
case R.id.button11:
clickNumber("9");
break;
case R.id.button13:
clickNumber(".");
break;
case R.id.button14:
clickNumber("0");
break;
case R.id.button4:
clickOperator("+");
break;
case R.id.button8:
clickOperator("-");
break;
case R.id.button12:
clickOperator("*");
break;
case R.id.button16:
clickOperator("/");
break;
case R.id.button15:
clickEqual();
break;
}
}

//将数字添加到输入框中
private void clickNumber(String number) {
mText += number;
EditText editText = (EditText) findViewById(R.id.et_input);
editText.setText(mText);
}

//处理点击运算符时的逻辑
private void clickOperator(String operator) {
if (mText != "") {
mOperator = operator;
mText += operator;
EditText editText = (EditText) findViewById(R.id.et_input);
editText.setText(mText);
}
}

//处理点击“=”时的逻辑
private void clickEqual() {
if (mOperator != "" && mText != "") {
double result = 0;
String[] arr = mText.split("\\" + mOperator);
if (arr.length == 2) {
double num1 = Double.parseDouble(arr[0]);
double num2

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/183045.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-24 16:26
下一篇 2024-11-24 16:26

相关推荐

  • Python基本索引用法介绍

    Python基本索引是指通过下标来获取列表、元组、字符串等数据类型中的元素。下面将从多个方面对Python基本索引进行详细的阐述。 一、列表(List)的基本索引 列表是Pytho…

    编程 2025-04-29
  • Python基本数字类型

    本文将介绍Python中基本数字类型,包括整型、布尔型、浮点型、复数型,并提供相应的代码示例以便读者更好的理解。 一、整型 整型即整数类型,Python中的整型没有大小限制,所以可…

    编程 2025-04-29
  • Python基本统计量计算

    本文将从多个方面详细介绍Python中基本统计量计算的方法。 一、均值 均值是一组数据的平均值,也就是将所有数据相加后再除以数据个数。 在Python中,可以使用numpy库中的m…

    编程 2025-04-29
  • Python程序的三种基本控制结构

    控制结构是编程语言中非常重要的一部分,它们指导着程序如何在不同的情况下执行相应的指令。Python作为一种高级编程语言,也拥有三种基本的控制结构:顺序结构、选择结构和循环结构。 一…

    编程 2025-04-29
  • Python三种基本输入元素

    本文将从多个方面对于Python三种基本输入元素进行详细的阐述并给出代码示例。 一、Python三种基本输入元素解答 Python三种基本输入元素包括命令行参数、标准输入和文件输入…

    编程 2025-04-28
  • 地理科学师范专业的教学应用与实践

    随着未来社会的发展,地理科学师范专业在我国的高等教育体系中扮演着越来越重要的角色。本文将从多个方面对该专业的教学应用与实践进行详细阐述,以期提高教师教学水平,增强学生学习体验。 一…

    编程 2025-04-27
  • Python基本操作:从入门到精通

    Python是一个功能强大的编程语言,有着简单易学的语法和广泛的用途。本篇文章将以Python基本操作为主要内容,从多个方面介绍Python的常用操作和技巧,帮助你快速学会Pyth…

    编程 2025-04-27
  • 汇率兑换计算器

    汇率兑换计算器是一款方便快捷的工具。它可以将一种货币的价值换算成另一种货币的价值,帮助人们更好地理解并计算不同货币之间的价格。 一、页面设计 汇率兑换计算器的页面设计应该简洁明了,…

    编程 2025-04-27
  • Python制作简易计算器

    本文将从多个方面,详细阐述如何使用Python制作简易计算器。 一、GUI界面设计 要制作一个简易计算器,我们需要先计划好它的GUI界面,以方便用户的使用。在Python中,我们可…

    编程 2025-04-27
  • 工期日历天计算器

    一、计算器介绍 工期日历天计算器是一款计算机程序,用于计算一个任务或项目的完成时间。 用户可以指定开始日期,工作日历和任务工期。该计算器能够自动排除非工作日和特殊工作日期,以提供客…

    编程 2025-04-24

发表回复

登录后才能评论