Kotlin TextWatcher 中没有按钮的 2 个数字相乘

问题描述

我的目标是使用 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()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...