所以我只是从Kotlin开始,所以我决定制作一个简单的计算器应用程序。计算器可以正常工作,但是当尝试进行新的计算时,即使清除了屏幕,它显然仍可以使用以前的值。因此,例如2 + 2将等于4。例如,如果尝试计算一个新数字,我将只单击CLS按钮,屏幕变黑。但是,问题是,如果我想计算类似2 + 3的值,它将不等于5,而是等于9。它被添加到先前的结果中。因此,即使清除后,占位符仍保留先前计算的值。

我的“清除屏幕”侦听器使用clear()清除屏幕上的数字,但不清除占位符。这是我的代码块


 val ClearButtonListener = View.OnClickListener { v ->
            val b = v as Button
            if (newNumber != null) {
                newNumber.getText().clear()
                result.getText().clear()
            }
        }


newNumber和result是EditText小部件,并使用lateinit声明。因此,我的问题是如何将这些小部件重置为null而不是保持先前的值?

我的整个代码:


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.NumberFormatException

class MainActivity : AppCompatActivity() {
    private lateinit var result: EditText
    private lateinit var newNumber: EditText
    private val displayOperation by lazy(LazyThreadSafetyMode.NONE) { findViewById<TextView>(R.id.operation) }

    private var operand1: Double? = null
    private var pendingOperation = "="

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        result = findViewById(R.id.result)
        newNumber = findViewById(R.id.newNumber)
        //Data input buttons
        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.buttonDecimal)

        val clearButton: Button = findViewById(R.id.clear)

        // operation buttons
        val Buttonequals: Button = findViewById(R.id.equal)
        val ButtonPlus: Button = findViewById(R.id.add)
        val ButtonMinus: Button = findViewById(R.id.Subtract)
        val ButtonMultiply: Button = findViewById(R.id.Multiply)
        val ButtonDivide: Button = findViewById(R.id.Divide)

        val listener = View.OnClickListener { v ->
            val b = v as Button
            newNumber.append(b.text)
        }

        //Clear button
        val ClearButtonListener = View.OnClickListener { v ->
            val b = v as Button
            if (newNumber != null) {
                newNumber.getText().clear()
                result.getText().clear()
            }
        }
        button0.setOnClickListener(listener)
        button1.setOnClickListener(listener)
        button2.setOnClickListener(listener)
        button3.setOnClickListener(listener)
        button4.setOnClickListener(listener)
        button5.setOnClickListener(listener)
        button6.setOnClickListener(listener)
        button7.setOnClickListener(listener)
        button8.setOnClickListener(listener)
        button9.setOnClickListener(listener)
        buttonDot.setOnClickListener(listener)

        clearButton.setOnClickListener(ClearButtonListener)

        val opListener = View.OnClickListener { v ->
            val op = (v as Button).text.toString()
            try {
                val value = newNumber.text.toString().toDouble()
                performOperation(value, op)
            } catch (e: NumberFormatException) {
                newNumber.setText("")
            }

            pendingOperation = op
            displayOperation.text = pendingOperation
        }

        Buttonequals.setOnClickListener(opListener)
        ButtonPlus.setOnClickListener(opListener)
        ButtonMinus.setOnClickListener(opListener)
        ButtonMultiply.setOnClickListener(opListener)
        ButtonDivide.setOnClickListener(opListener)
    }
    private fun performOperation(value: Double, operation: String) {
        if (operand1 == null) {
            operand1 = value
        } else {


            if (pendingOperation == "=") {
                pendingOperation = operation
            }

            when (pendingOperation) {
                "=" -> operand1 = value
                "+" -> operand1 = operand1!! + value
                "-" -> operand1 = operand1!! - value
                "*" -> operand1 = operand1!! * value
                "/" -> if (value == 0.0) {
                    operand1 = Double.NaN
                } else {
                    operand1 = operand1!! / value
                }
            }
        }
        result.setText(operand1.toString())
        newNumber.setText("")
    }
}

最佳答案

您永远不会重置operand1。因此,获得第一个结果后,不会到达performOperation的第二行。然后,operand1仍会保留您在when子句中为=设置的最后一个值。
result.setText(operand1.toString())之后,致电

if (pendingOperation == "=") {
    operand1 = null
}


那应该解决它。

07-24 09:26