我的應用程式中有一個 numberDecimal EditText (codeInput),我希望輸出在輸入時乘以 double (tipPercent),這樣我就可以將它顯示到 textView (totalText)。我試過這個代碼:
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)
}
})
但是,當我使用它時,出現此錯誤:
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
我能做些什么來解決這個問題?謝謝。
uj5u.com熱心網友回復:
override fun onTextChanged(s: CharSequence, start: Int,
before: Int, count: Int) {
val input = Integer.parseInt(s.toString())
val res = input * tipPercent
totalText.setText("$res")
}
uj5u.com熱心網友回復:
嘗試這個:
costInput.doAfterTextChanged { text ->
val cost = text.toString().toDoubleOrNull()
totalText.text = if(cost == null) "" else totalText.text = "${cost * tipPercent}"
}
這將處理 costInput 中的無效輸入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359225.html
上一篇:在SwiftUI中回圈遍歷物體陣列以基于陣列資料中的值構建樹結構視圖
下一篇:實施回收配接器時應用程式崩潰
