问题描述
我的目标是使用 2 个 edittext 自动显示用户在下面的 textView 中输入的数字,如下所示。我该怎么做?因为这是暗恋。如果您详细解释,我将不胜感激。提前致谢。
items3=pronumber.text.toString() // Edittext
items4=proprice.text.toString()// Edittext
pronumber.addTextChangedListener ( object:TextWatcher{
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?,p1: Int,p2: Int,p3: Int) {
Todo("Not yet implemented")
}
override fun onTextChanged(p0: CharSequence?,p3: Int) {
totalprice.text= "Toplam Tutar : "+(items3.toInt()*items4.toInt()).toString() +" TL" //TextView
}
} )
proprice.addTextChangedListener ( object:TextWatcher{
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?,p3: Int) {
totalprice.text= "Toplam Tutar : "+(items3.toInt()*items4.toInt()).toString() +" TL" //TextView
}
} )
items5=totalprice.text.toString()
解决方法
你应该像这样改变你的代码
items3=pronumber.text.toString() // Edittext
items4=proprice.text.toString()// Edittext
pronumber.addTextChangedListener ( object:TextWatcher{
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?,p1: Int,p2: Int,p3: Int) {
}
override fun onTextChanged(p0: CharSequence?,p3: Int) {
val first = pronumber.text.toString()
val second = proprice.text.toString()
totalprice.text = if (first.isBlank() || second.isBlank()){
"Toplam Tutar : 0 TL"
} else {
"Toplam Tutar : ${first.toInt().times(second.toInt())} TL"
}
}
} )
proprice.addTextChangedListener ( object:TextWatcher{
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?,p3: Int) {
totalprice.text = if (first.isBlank() || second.isBlank()){
"Toplam Tutar : 0 TL"
} else {
"Toplam Tutar : ${first.toInt().times(second.toInt())} TL"
}
}
} )
items5=totalprice.text.toString()