用Kotlin编写一个apk简单计算器
时间: 2023-08-21 17:04:17 浏览: 58
好的,以下是一个简单的计算器的 Kotlin 代码:
```Kotlin
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var resultTextView: TextView
private var operand1: Double = 0.0
private var operand2: Double = 0.0
private var operation: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
resultTextView = findViewById(R.id.resultTextView)
val button0: Button = findViewById(R.id.button0)
val button1: Button = findViewById(R.id.button1)
val button2: Button = findViewById(R.id.button2)
val button3: Button = findViewById(R.id.button3)
val button4: Button = findViewById(R.id.button4)
val button5: Button = findViewById(R.id.button5)
val button6: Button = findViewById(R.id.button6)
val button7: Button = findViewById(R.id.button7)
val button8: Button = findViewById(R.id.button8)
val button9: Button = findViewById(R.id.button9)
val buttonDot: Button = findViewById(R.id.buttonDot)
val buttonEquals: Button = findViewById(R.id.buttonEquals)
val buttonPlus: Button = findViewById(R.id.buttonPlus)
val buttonMinus: Button = findViewById(R.id.buttonMinus)
val buttonMultiply: Button = findViewById(R.id.buttonMultiply)
val buttonDivide: Button = findViewById(R.id.buttonDivide)
val buttonClear: Button = findViewById(R.id.buttonClear)
button0.setOnClickListener { numberPressed("0") }
button1.setOnClickListener { numberPressed("1") }
button2.setOnClickListener { numberPressed("2") }
button3.setOnClickListener { numberPressed("3") }
button4.setOnClickListener { numberPressed("4") }
button5.setOnClickListener { numberPressed("5") }
button6.setOnClickListener { numberPressed("6") }
button7.setOnClickListener { numberPressed("7") }
button8.setOnClickListener { numberPressed("8") }
button9.setOnClickListener { numberPressed("9") }
buttonDot.setOnClickListener { numberPressed(".") }
buttonPlus.setOnClickListener { operationPressed("+") }
buttonMinus.setOnClickListener { operationPressed("-") }
buttonMultiply.setOnClickListener { operationPressed("*") }
buttonDivide.setOnClickListener { operationPressed("/") }
buttonEquals.setOnClickListener { equalsPressed() }
buttonClear.setOnClickListener { clearPressed() }
}
private fun numberPressed(number: String) {
val currentText = resultTextView.text.toString()
resultTextView.text = currentText + number
}
private fun operationPressed(operation: String) {
operand1 = resultTextView.text.toString().toDouble()
resultTextView.text = ""
this.operation = operation
}
private fun equalsPressed() {
operand2 = resultTextView.text.toString().toDouble()
val result: Double = when (operation) {
"+" -> operand1 + operand2
"-" -> operand1 - operand2
"*" -> operand1 * operand2
"/" -> operand1 / operand2
else -> 0.0
}
resultTextView.text = result.toString()
}
private fun clearPressed() {
resultTextView.text = ""
operand1 = 0.0
operand2 = 0.0
operation = ""
}
}
```
在这个代码中,我们定义了一个 `MainActivity` 类,继承自 `AppCompatActivity`。在 `MainActivity` 类中,我们定义了一些私有变量和函数:
- `resultTextView`:显示计算结果的文本视图。
- `operand1` 和 `operand2`:操作数1和操作数2。
- `operation`:操作符。
- `numberPressed(number: String)`:用户按下数字按钮时被调用的函数,将数字添加到 `resultTextView` 中。
- `operationPressed(operation: String)`:用户按下操作符按钮时被调用的函数,将 `resultTextView` 中的数字作为操作数1,并清空 `resultTextView`,保存当前的操作符。
- `equalsPressed()`:用户按下等号按钮时被调用的函数,将 `resultTextView` 中的数字作为操作数2,并根据当前的操作符计算结果,将结果显示在 `resultTextView` 中。
- `clearPressed()`:用户按下清除按钮时被调用的函数,清空 `resultTextView`,操作数1和操作数2,以及操作符。
在 `onCreate` 函数中,我们获取了所有的按钮,并设置了它们的点击事件监听器。当用户按下数字按钮时,我们调用 `numberPressed` 函数;当用户按下操作符按钮时,我们调用 `operationPressed` 函数;当用户按下等号按钮时,我们调用 `equalsPressed` 函数;当用户按下清除按钮时,我们调用 `clearPressed` 函数。
这个简单的计算器应该能够满足大多数基本计算需求,但是它并不具备一些高级功能,如括号、求幂等。如果您需要这些功能,可以根据这个代码进行扩展。