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.
CodePudding user response:
override fun onTextChanged(s: CharSequence, start: Int,
before: Int, count: Int) {
val input = Integer.parseInt(s.toString())
val res = input * tipPercent
totalText.setText("$res")
}
CodePudding user response:
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.