大學生Android實驗:從零搭建簡單計算器應用

一、前言

隨著智能手機的普及,手機應用的開發成為了許多人關注的焦點。而Android作為最為流行的操作系統之一,其應用的開發也越來越受到大眾的關注。本篇文章將帶領初學者從零開始搭建一個簡單的計算器應用,並講解其中的一些基礎知識。

二、開發環境配置

在開始編寫代碼之前,我們需要配置好Android開發環境。首先要下載安裝Android Studio,該軟體集成了一系列Android開發所需要的工具和庫,並提供了強大的IDE和調試功能。

在安裝完Android Studio之後,我們還需要在SDK Manager中下載所需的Android SDK和Android Virtual Device。其中Android SDK是開發Android應用所必須的一些庫和工具,而Android Virtual Device是用於模擬Android設備的虛擬機。

安裝好所需的工具之後,我們就可以開始開發我們的第一個Android應用了。

三、UI設計

計算器應用的主要界面需要包括數字按鍵、運算符號按鍵、清除按鈕以及結果顯示區域。我們需要在res/layout目錄下創建一個新的xml文件,來定義應用的UI界面。以下是一個例子:

<?xml version="1.0" encoding="utf-8"?>
<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/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textStyle="bold"
        android:gravity="right"
        android:padding="16dp" />

    <LinearLayout
        android:layout_width="match_parent"
        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:layout_weight="1" />

        <Button
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            android:layout_weight="1" />

        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="1" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            android:layout_weight="1" />

        <Button
            android:id="@+id/subtract"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:layout_weight="1" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn_8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn_9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9"
            android:layout_weight="1" />

        <Button
            android:id="@+id/multiply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*"
            android:layout_weight="1" />

    </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="C"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn_0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_weight="1" />

        <Button
            android:id="@+id/equal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="="
            android:layout_weight="1" />

        <Button
            android:id="@+id/divide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"
            android:layout_weight="1" />

    </LinearLayout>

</LinearLayout>

以上代碼定義了一個線性布局,首先包含了一個TextView用於顯示計算結果。接下來分幾行定義了數字按鍵、運算符號按鍵和一個清除按鈕的水平布局,其中每個按鍵都使用了Button控制項,並通過layout_weight屬性來使它們平均分配布局空間,並填滿整行。最後再定義一個包含「/」符號的運算符按鍵以及一個包含「=」符號的等於按鈕,它們都與數字按鍵和其他運算符按鍵一樣。

四、Java代碼實現

可以在res/layout文件夾中的xml文件中定義應用的UI界面,但是如果要使應用具有一定的功能,就需要在Java代碼中實現它們。以下是一個簡單的實現:

package com.example.calculator;

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 result;
    private double num1, num2;
    private String operator;

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

    private void initView() {
        result = findViewById(R.id.result);
        findViewById(R.id.btn_0).setOnClickListener(this);
        findViewById(R.id.btn_1).setOnClickListener(this);
        findViewById(R.id.btn_2).setOnClickListener(this);
        findViewById(R.id.btn_3).setOnClickListener(this);
        findViewById(R.id.btn_4).setOnClickListener(this);
        findViewById(R.id.btn_5).setOnClickListener(this);
        findViewById(R.id.btn_6).setOnClickListener(this);
        findViewById(R.id.btn_7).setOnClickListener(this);
        findViewById(R.id.btn_8).setOnClickListener(this);
        findViewById(R.id.btn_9).setOnClickListener(this);
        findViewById(R.id.add).setOnClickListener(this);
        findViewById(R.id.subtract).setOnClickListener(this);
        findViewById(R.id.multiply).setOnClickListener(this);
        findViewById(R.id.divide).setOnClickListener(this);
        findViewById(R.id.btn_clear).setOnClickListener(this);
        findViewById(R.id.equal).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
                Button btn = (Button) v;
                String str = btn.getText().toString();
                result.append(str);
                break;
            case R.id.add:
            case R.id.subtract:
            case R.id.multiply:
            case R.id.divide:
                Button btnOperator = (Button) v;
                num1 = Double.parseDouble(result.getText().toString());
                operator = btnOperator.getText().toString();
                result.setText("");
                break;
            case R.id.btn_clear:
                result.setText("");
                break;
            case R.id.equal:
                num2 = Double.parseDouble(result.getText().toString());
                double resultNum = 0;
                switch (operator) {
                    case "+":
                        resultNum = num1 + num2;
                        break;
                    case "-":
                        resultNum = num1 - num2;
                        break;
                    case "*":
                        resultNum = num1 * num2;
                        break;
                    case "/":
                        if (num2 == 0) {
                            resultNum = Double.NaN;
                        } else {
                            resultNum = num1 / num2;
                        }
                        break;
                }
                result.setText(String.valueOf(resultNum));
                break;
            default:
                break;
        }
    }
}

在MainActivity類中,我們定義了許多變數和方法。num1和num2是用於記錄計算操作數字的雙精度浮點數,而operator是用於記錄所使用的運算符的字元串。initView()方法初始化了控制項,並為每個數字按鈕、每個運算符號按鈕、清除按鈕和等於按鈕添加點擊事件監聽器。

點擊事件onClick(View v)方法中定義了按鈕的點擊事件處理邏輯。當點擊數字按鈕時,獲取其文本並將其追加到結果顯示區域上;當點擊運算符按鈕時,將結果顯示區域的內容轉換為num1,將運算符記錄在operator中,並將結果顯示區域清空;當點擊清除按鈕時,可以清除結果顯示區域的內容;當點擊等於按鈕時,將結果顯示區域中的內容轉換為num2,根據所使用的運算符計算結果,並將計算結果顯示在結果顯示區域上。

五、應用體驗

打開模擬器或在真實的Android設備上運行這個計算器應用,您將得到以下界面:

在這個演示應用中,可以執行加、減、乘和除四種操作,並可以執行連續的操作,最終顯示計算結果。

六、總結

本篇文章講解了開發一個簡單計算器應用所需的基本知識。通過XML定義UI界面和Java代碼實現功能邏輯,我們可以在Android操作系統上構建出一個完整而有用的應用。

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

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

相關推薦

  • Python簡單數學計算

    本文將從多個方面介紹Python的簡單數學計算,包括基礎運算符、函數、庫以及實際應用場景。 一、基礎運算符 Python提供了基礎的算術運算符,包括加(+)、減(-)、乘(*)、除…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 如何寫好大學生周記1000字?

    大學生周記1000字是一項任務,也是一次鍛煉。如何寫好大學生周記1000字?從以下幾個方面來探討。 一、清晰表達 清晰表達是任何一篇文章的重要要素,大學生周記1000字也不例外。在…

    編程 2025-04-29
  • Python海龜代碼簡單畫圖

    本文將介紹如何使用Python的海龜庫進行簡單畫圖,並提供相關示例代碼。 一、基礎用法 使用Python的海龜庫,我們可以控制一個小海龜在窗口中移動,並利用它的「畫筆」在窗口中繪製…

    編程 2025-04-29
  • Python櫻花樹代碼簡單

    本文將對Python櫻花樹代碼進行詳細的闡述和講解,幫助讀者更好地理解該代碼的實現方法。 一、簡介 櫻花樹是一種圖形效果,它的實現方法比較簡單。Python中可以通過turtle這…

    編程 2025-04-28
  • Python大神作品:讓編程變得更加簡單

    Python作為一種高級的解釋性編程語言,一直被廣泛地運用於各個領域,從Web開發、遊戲開發到人工智慧,Python都扮演著重要的角色。Python的代碼簡潔明了,易於閱讀和維護,…

    編程 2025-04-28
  • Android ViewPager和ScrollView滑動衝突問題

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

    編程 2025-04-28
  • 用Python實現簡單爬蟲程序

    在當今時代,互聯網上的信息量是爆炸式增長的,其中很多信息可以被利用。對於數據分析、數據挖掘或者其他一些需要大量數據的任務,我們可以使用爬蟲技術從各個網站獲取需要的信息。而Pytho…

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

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

    編程 2025-04-28
  • 如何製作一個簡單的換裝遊戲

    本文將從以下幾個方面,為大家介紹如何製作一個簡單的換裝遊戲: 1. 遊戲需求和界面設計 2. 使用HTML、CSS和JavaScript開發遊戲 3. 實現遊戲的基本功能:拖拽交互…

    編程 2025-04-27

發表回復

登錄後才能評論