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/zh-tw/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

發表回復

登錄後才能評論