我无法将字符串值保存到计算器应用程序的 StringBuilder 变量中

I can't save a string value into StringBuilder variable on my calcaultor app

我一直在开发一个计算器应用程序,我设法完成了所有按钮,但退格键不起作用,每次我尝试它基本上它都会从 textView 中删除数字并且它不会保存它的真实值例如,如果我按 9 按钮 3 次,结果是“999”,然后我按退格按钮,它应该是“99”,而实际发生的情况仅适用于 textView 因此,如果我在使用退格键“999”后尝试将“99”乘以“1”,实际给出的结果是“999”,这是错误的,因为“99”*“1”=“99”

我这里是我的mainActivity代码的一部分:

package com.example.newcal

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils.substring
import android.view.View
import android.widget.Button
import android.widget.TextView
import java.lang.IllegalArgumentException
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.absoluteValue


class MainActivity : AppCompatActivity() {
    private var strNumber = StringBuilder()
    private lateinit var tvDisplay: TextView
    private lateinit var button1: Button
    private lateinit var button2: Button
    private lateinit var button3: Button
    private lateinit var button4: Button
    private lateinit var button5: Button
    private lateinit var button6: Button
    private lateinit var button7: Button
    private lateinit var button8: Button
    private lateinit var button9: Button
    private lateinit var buttonClear: Button
    private lateinit var buttonDot: Button
    private lateinit var buttonZero: Button
    private lateinit var buttonDivide: Button
    private lateinit var buttonMultiply: Button
    private lateinit var buttonSubtract: Button
    private lateinit var buttonAdd: Button
    private lateinit var buttonEquals: Button
    private lateinit var buttonBack: Button
    private lateinit var operatorButtons: List<Button>
    private lateinit var numbButtons: Array<Button>
    private var operator: Operator = Operator.NONE
    private var isOperatorClicked: Boolean = false
    private var no1: Double = 0.0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        components()

        strNumber.append(0.0)
        tvDisplay.text = strNumber
        isOperatorClicked = true

        for (i in operatorButtons){i.setOnClickListener { operatorButtonClicked(i) }}
        for (i in numbButtons){i.setOnClickListener { numbButtonsClicked(i) }}
        buttonEquals.setOnClickListener { buttonEqualsClicked() }
        buttonClear.setOnClickListener { btnClickedClear() }
        buttonBack.setOnClickListener { btnClickedBack() }

    }

private fun buttonEqualsClicked() {
        val no2 = strNumber.toString().toDouble()
        val result: Double
            if (isOperatorClicked == false){
                when(operator){
                    Operator.ADD -> result = no1 + no2
                    Operator.SUB -> result = no1 - no2
                    Operator.DIV -> result = no1 / no2
                    Operator.MUL -> result = no1 * no2
                    Operator.NONE -> result = 0.0
                }
                strNumber.clear()
                strNumber.append(result.toString())
                tvDisplay.text = strNumber
                isOperatorClicked = true
            }

        }

 private fun btnClickedBack() {
        var str = tvDisplay.text.toString()
        if (str.length > 1) {
            str = str.substring(0, str.length - 1)
            tvDisplay.text = str

            }
        if (str.length <= 1){
            val st = strNumber.append(btnClickedClear().substring(0, strNumber.length -3))
            tvDisplay.text = st
        }

    }

enum class Operator{ADD, SUB, DIV, MUL, NONE}

这里有一些屏幕截图,以便更好地查看

https://imgur.com/a/glhCP0f

在我看来,您不必使用 StringBuilder。相反,您可以使用您将直接修改的字符串变量。

如果您确定要继续使用 StringBuilder,则必须删除 StringBuilder 中的最后一个字符,如下所示。

 private fun btnClickedBack() {
    if(strNumber.length() > 0){
       strNumber.deleteCharAt(sb.length() - 1)
       tvDisplay.text = strNumber
    }
}