我是 Kotlin 语言的新手。任何人都可以帮助如何添加显示在不同 onClickListener 上的两个总值吗?

赖。灾难
var addQuantity = 0     // counter variable

 // button_two on clickListener 
button_two.setOnClickListener{                                     
    val laptopTwo = 500.60
    val total_price_two: Double
    addQuantity = addQuantity + 1
    total_price_two = (addQuantity * laptopTwo).toDouble()
    resultDisplayText.text = total_price_two.toString()       //displays the result in TextView
}

 // button_three on clickListener 
button_three.setOnClickListener {
    val laptopThree=400.00
    val total_price_three:Double
    addQuantity = addQuantity + 1
    total_price_three =( addQuantity * laptopThree).toDouble()
    resultDisplayText.text=total_price_three.toString() //displays the result in TextView 
}

//submit button on clickListener
submit.setOnClickListener {                                              
    val total: Double
    total = total_price_three + total_price_two // get an error here....
}

每次我点击button_twobutton_three,它都会增加数量并给出数量的总价。现在我想在点击提交按钮时显示总价,但它不起作用。谁能帮我解决这个问题。

斯特凡·热利亚兹科夫

submit按钮无权访问total_price_two并且total_price_three因为它们在本地范围限定为两个按钮的点击侦听器。如果您将它们拉入外部作用域,它们将是可访问的:

var addQuantity = 0     // counter variable
var total_price_two: Double = 0.0
var total_price_three: Double = 0.0

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章