val textWatcher: TextWatcher = object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
var s = s
if (s.toString().contains(".")) {
if (s.length - 1 - s.toString().indexOf(".") > 2) {
s = s.toString().subSequence(0,
s.toString().indexOf(".") + 3)
et_course_price.setText(s)
et_course_price.setSelection(s.length)
}
}
if (s.toString().trim { it <= ' ' }.substring(0) == ".") {
s = "0$s"
et_course_price.setText(s)
et_course_price.setSelection(2)
}
if (s.toString().startsWith("0")
&& s.toString().trim { it <= ' ' }.length > 1) {
if (s.toString().substring(1, 2) != ".") {
et_course_price.setText(s.subSequence(0, 1))
et_course_price.setSelection(1)
return
}
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int){}
override fun afterTextChanged(s: Editable?) {}
}
et_course_price.addTextChangedListener(textWatcher)
|