android studio使用kotlin的gridlayout的布局实现计算器功能
时间: 2023-08-12 21:20:53 浏览: 205
android studio实现计算器
5星 · 资源好评率100%
好的,以下是实现计算器功能的步骤:
1. 在 Android Studio 中创建一个新的 Kotlin 项目。
2. 打开 activity_main.xml 文件,在根布局中添加一个 GridLayout 控件,并设置它的列数为 4。
```xml
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4">
</GridLayout>
```
3. 在 GridLayout 中添加按钮,分别代表数字、运算符和其他功能,例如清除、删除和等于号。
```xml
<Button
android:id="@+id/btn_clear"
android:text="C"
android:layout_column="0"
android:layout_row="0"
android:layout_columnSpan="2" />
<Button
android:id="@+id/btn_delete"
android:text="DEL"
android:layout_column="2"
android:layout_row="0" />
<Button
android:id="@+id/btn_divide"
android:text="/"
android:layout_column="3"
android:layout_row="0" />
<Button
android:id="@+id/btn_7"
android:text="7"
android:layout_column="0"
android:layout_row="1" />
<Button
android:id="@+id/btn_8"
android:text="8"
android:layout_column="1"
android:layout_row="1" />
<Button
android:id="@+id/btn_9"
android:text="9"
android:layout_column="2"
android:layout_row="1" />
<Button
android:id="@+id/btn_multiply"
android:text="*"
android:layout_column="3"
android:layout_row="1" />
<Button
android:id="@+id/btn_4"
android:text="4"
android:layout_column="0"
android:layout_row="2" />
<Button
android:id="@+id/btn_5"
android:text="5"
android:layout_column="1"
android:layout_row="2" />
<Button
android:id="@+id/btn_6"
android:text="6"
android:layout_column="2"
android:layout_row="2" />
<Button
android:id="@+id/btn_minus"
android:text="-"
android:layout_column="3"
android:layout_row="2" />
<Button
android:id="@+id/btn_1"
android:text="1"
android:layout_column="0"
android:layout_row="3" />
<Button
android:id="@+id/btn_2"
android:text="2"
android:layout_column="1"
android:layout_row="3" />
<Button
android:id="@+id/btn_3"
android:text="3"
android:layout_column="2"
android:layout_row="3" />
<Button
android:id="@+id/btn_plus"
android:text="+"
android:layout_column="3"
android:layout_row="3" />
<Button
android:id="@+id/btn_0"
android:text="0"
android:layout_column="0"
android:layout_row="4"
android:layout_columnSpan="2" />
<Button
android:id="@+id/btn_dot"
android:text="."
android:layout_column="2"
android:layout_row="4" />
<Button
android:id="@+id/btn_equal"
android:text="="
android:layout_column="3"
android:layout_row="4" />
```
4. 在 MainActivity.kt 文件中,设置按钮的点击事件,并实现计算器的逻辑。
```kotlin
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.children
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), View.OnClickListener {
private var input = ""
private var result = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_clear.setOnClickListener(this)
btn_delete.setOnClickListener(this)
btn_divide.setOnClickListener(this)
btn_multiply.setOnClickListener(this)
btn_minus.setOnClickListener(this)
btn_plus.setOnClickListener(this)
btn_dot.setOnClickListener(this)
btn_equal.setOnClickListener(this)
gridLayout.children.forEach {
if (it is Button) {
it.setOnClickListener(this)
}
}
}
override fun onClick(view: View?) {
when (view?.id) {
R.id.btn_clear -> {
input = ""
result = ""
updateResult()
}
R.id.btn_delete -> {
if (input.isNotEmpty()) {
input = input.substring(0, input.length - 1)
updateResult()
}
}
R.id.btn_divide -> {
handleOperator("/")
}
R.id.btn_multiply -> {
handleOperator("*")
}
R.id.btn_minus -> {
handleOperator("-")
}
R.id.btn_plus -> {
handleOperator("+")
}
R.id.btn_dot -> {
if (input.isNotEmpty() && !input.contains(".")) {
input += "."
updateResult()
}
}
R.id.btn_equal -> {
if (input.isNotEmpty() && result.isNotEmpty()) {
val expression = "$result$input"
val answer = Expression(expression).calculate()
result = answer.toString()
input = ""
updateResult()
}
}
else -> {
val digit = (view as Button).text.toString()
input += digit
updateResult()
}
}
}
private fun handleOperator(operator: String) {
if (input.isNotEmpty()) {
result = if (result.isNotEmpty()) {
val expression = "$result$input"
val answer = Expression(expression).calculate()
input = ""
answer.toString()
} else {
input
}
input = ""
result += operator
updateResult()
}
}
private fun updateResult() {
tv_result.text = if (result.isNotEmpty()) {
"$result$input"
} else {
input
}
}
}
```
5. 在 MainActivity.kt 文件中,添加一个名为 "Expression" 的类,用于计算表达式的值。
```kotlin
import java.util.*
class Expression(val expression: String) {
private val operators: Stack<String> = Stack()
private val operands: Stack<Double> = Stack()
fun calculate(): Double {
val tokens = expression.split(" ")
tokens.forEach { token ->
when {
isNumber(token) -> {
operands.push(token.toDouble())
}
isOperator(token) -> {
while (operators.isNotEmpty() && precedence(operators.peek()) >= precedence(token)) {
val operator = operators.pop()
val operand2 = operands.pop()
val operand1 = operands.pop()
operands.push(applyOperator(operator, operand1, operand2))
}
operators.push(token)
}
token == "(" -> {
operators.push(token)
}
token == ")" -> {
while (operators.peek() != "(") {
val operator = operators.pop()
val operand2 = operands.pop()
val operand1 = operands.pop()
operands.push(applyOperator(operator, operand1, operand2))
}
operators.pop()
}
}
}
while (operators.isNotEmpty()) {
val operator = operators.pop()
val operand2 = operands.pop()
val operand1 = operands.pop()
operands.push(applyOperator(operator, operand1, operand2))
}
return operands.pop()
}
private fun isNumber(token: String): Boolean {
return try {
token.toDouble()
true
} catch (e: NumberFormatException) {
false
}
}
private fun isOperator(token: String): Boolean {
return token == "+" || token == "-" || token == "*" || token == "/"
}
private fun precedence(token: String): Int {
return when (token) {
"+", "-" -> 1
"*", "/" -> 2
else -> 0
}
}
private fun applyOperator(operator: String, operand1: Double, operand2: Double): Double {
return when (operator) {
"+" -> operand1 + operand2
"-" -> operand1 - operand2
"*" -> operand1 * operand2
"/" -> operand1 / operand2
else -> 0.0
}
}
}
```
以上就是使用 Kotlin 和 GridLayout 布局实现计算器功能的步骤。
阅读全文