一、計算器介紹
計算器是我們日常生活中經常使用的工具之一。在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-hk/n/257840.html
微信掃一掃
支付寶掃一掃