一、前言
隨著智能手機的普及,手機應用的開發成為了許多人關注的焦點。而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