skip to Main Content

I have a numberDecimal EditText (codeInput) in my app, and I want the output to be multiplied by a double (tipPercent) as it is being typed, so I can display it to a textView (totalText). I tried this code:

costInput.addTextChangedListener(object : TextWatcher{
            override fun afterTextChanged(s: Editable) {}

            override fun beforeTextChanged(s: CharSequence, start: Int,
                                           count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int,
                                       before: Int, count: Int) {
                totalText.setText(s * tipPercent)
            }
        })

when I use this, however, I get this error:

    Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public inline operator fun BigDecimal.plus(other: BigDecimal): BigDecimal defined in kotlin
public inline operator fun BigInteger.plus(other: BigInteger): BigInteger defined in kotlin

what can I do to fix this? Thanks.

2

Answers


  1. override fun onTextChanged(s: CharSequence, start: Int,
                                           before: Int, count: Int) {
                    val input = Integer.parseInt(s.toString())
                    val res = input * tipPercent
                    totalText.setText("$res")
                }
    
    Login or Signup to reply.
  2. Try this:

    costInput.doAfterTextChanged { text ->
        val cost = text.toString().toDoubleOrNull()
        totalText.text = if(cost == null) "" else totalText.text = "${cost * tipPercent}"
    }
    

    This will take care of invalid input in costInput.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search