Android Studio製作實用的計算器

一、計算器介紹

計算器是我們日常生活中經常使用的工具之一。在Android系統中,也有許多不同版本的計算器應用。今天,我們將使用Android Studio來打造一個實用的計算器應用。

二、構建計算器UI界面

在開始之前,我們需要先構建計算器的UI界面。在Android Studio中使用布局文件可以快速構建我們想要的UI界面。在這個例子中,我們使用線性布局LinearLayout來實現。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- 顯示計算結果的文本框-->
            <TextView
                android:id="@+id/tv_result"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:gravity="right"
                android:textColor="#000000"
                android:text="0"/>

            <!-- 操作按鈕區域-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

            <!-- 數字按鈕-->
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/btn_1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="1"
                        android:textSize="20sp"/>

                    <!-- 其他數字按鈕省略 -->

                </LinearLayout>

            <!-- 運算符按鈕-->
            < LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                < Button
                    android:id="@+id/btn_add"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="+"
                    android:textSize="20sp"/>

                <!-- 其他運算符按鈕省略-->

            </ LinearLayout>

            </LinearLayout>

            <!-- 清除和計算按鈕-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                < Button
                    android:id="@+id/btn_clear"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="CLEAR"
                    android:textSize="20sp"/>


                < Button
                    android:id="@+id/btn_calc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="="
                    android:textSize="20sp"/>

            </ LinearLayout>

        </LinearLayout>

三、實現UI交互邏輯

接下來,我們需要為按鈕添加點擊事件來實現計算器的功能。我們需要在Activity中獲取到按鈕的實例,並且為每個按鈕設置點擊事件。在這個例子中,我們使用Kotlin語言來實現計算器的邏輯判斷。

    class MainActivity : AppCompatActivity() {

        private var result: String = ""
        private var temp: String = ""
        private var operator: String = ""

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            // 獲取數字和運算符按鈕
            val tvResult = findViewById<TextView>(R.id.tv_result)
            val btn1 = findViewById<Button>(R.id.btn_1)
            val btn2 = findViewById<Button>(R.id.btn_2)
            val btn3 = findViewById<Button>(R.id.btn_3)
            // 其他數字按鈕省略
            val btnAdd = findViewById<Button>(R.id.btn_add)
            val btnSub = findViewById<Button>(R.id.btn_sub)
            // 其他運算符按鈕省略
            val btnClear = findViewById<Button>(R.id.btn_clear)
            val btnCalc = findViewById<Button>(R.id.btn_calc)

            // 數字按鈕的點擊事件
            btn1.setOnClickListener {
                if (result == "0") {
                    result = "1"
                } else {
                    result = result + "1"
                }
                tvResult.text = result
            }
            // 其他數字按鈕點擊事件省略

            // 運算符按鈕的點擊事件
            btnAdd.setOnClickListener {
                temp = result
                result = ""
                operator = "+"
            }
            btnSub.setOnClickListener {
                temp = result
                result = ""
                operator = "-"
            }
            // 其他運算符按鈕點擊事件省略

            // 清空按鈕的點擊事件
            btnClear.setOnClickListener {
                result = "0"
                temp = ""
                operator = ""
                tvResult.text = result
            }

            // 計算按鈕的點擊事件
            btnCalc.setOnClickListener {
                val x = temp.toDouble()
                val y = result.toDouble()
                when (operator) {
                    "+" -> result = (x + y).toString()
                    "-" -> result = (x - y).toString()
                    // 其他運算符計算邏輯省略
                }
                tvResult.text = result
            }
        }
    }

四、測試運行

在代碼編寫完成後,我們需要測試運行我們的計算器應用。在Android Studio中可以使用虛擬設備或實機進行測試。在這個例子中,我們使用的是實機進行測試。

在運行App之前,我們需要先連接手機或模擬器。在連接成功後運行App,即可在手機屏幕上查看已經成功創建的計算器應用。

五、總結

通過這個例子,我們學習了使用Android Studio來創建一個實用的計算器應用。在實現過程中,我們學習了Android Studio的布局布局文件和Kotlin語言的使用,以及如何通過模擬器或實機進行測試和運行。我們相信這個例子可以幫助你了解Android Studio的使用和Android應用開發的知識點。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/257840.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-15 12:47
下一篇 2024-12-15 12:47

相關推薦

  • Android ViewPager和ScrollView滑動衝突問題

    Android開發中,ViewPager和ScrollView是兩個常用的控制項。但是當它們同時使用時,可能會發生滑動衝突的問題。本文將從多個方面介紹解決Android ViewPa…

    編程 2025-04-28
  • Android如何點擊其他區域收起軟鍵盤

    在Android應用中,當輸入框獲取焦點彈出軟鍵盤後,我們希望能夠點擊其他區域使軟鍵盤消失,以提升用戶體驗。本篇文章將說明如何實現這一功能。 一、獲取焦點並顯示軟鍵盤 在Andro…

    編程 2025-04-28
  • Android Studio HUD 實現指南

    本文將會以實例來詳細闡述如何在 Android Studio 中使用 HUD 功能實現菊花等待指示器的效果。 一、引入依賴庫 首先,我們需要在 build.gradle 文件中引入…

    編程 2025-04-27
  • 匯率兌換計算器

    匯率兌換計算器是一款方便快捷的工具。它可以將一種貨幣的價值換算成另一種貨幣的價值,幫助人們更好地理解並計算不同貨幣之間的價格。 一、頁面設計 匯率兌換計算器的頁面設計應該簡潔明了,…

    編程 2025-04-27
  • Android和Vue3混合開發方案

    本文將介紹如何將Android和Vue3結合起來進行混合開發,以及其中的優勢和注意事項。 一、環境搭建 在進行混合開發之前,需要搭建好相應的開發環境。首先需要安裝 Android …

    編程 2025-04-27
  • Android Java Utils 可以如何提高你的開發效率

    Android Java Utils 是一款提供了一系列方便實用的工具類的 Java 庫,可以幫助開發者更加高效地進行 Android 開發,提高開發效率。本文將從以下幾個方面對 …

    編程 2025-04-27
  • Python製作簡易計算器

    本文將從多個方面,詳細闡述如何使用Python製作簡易計算器。 一、GUI界面設計 要製作一個簡易計算器,我們需要先計劃好它的GUI界面,以方便用戶的使用。在Python中,我們可…

    編程 2025-04-27
  • Android JUnit測試完成程序自動退出決方法

    對於一些Android JUnit測試的開發人員來說,程序自動退出是一個經常面臨的困擾。下面從多個方面給出解決方法。 一、檢查測試代碼 首先,我們應該仔細檢查我們的測試代碼,確保它…

    編程 2025-04-25
  • Android Activity啟動流程

    一、Activity概述 Android應用程序是由許多Activity組成的。一個Activity代表一個屏幕上的窗口。用戶與應用程序交互時,Activity會接收用戶的輸入並處…

    編程 2025-04-25
  • 對 Rad Studio 11.1 進行詳細闡述

    一、新特性 Rad Studio 11.1 是一款全面集成的軟體開發環境,主要服務於 Windows、macOS、iOS 和 Android 四個平台。其中,最引人注目的是其新特性…

    編程 2025-04-25

發表回復

登錄後才能評論