[英]How to set value limit in edittext 0 to 300
i want to set value limit in edittext not character limit in edit text.我想在编辑文本中设置值限制而不是在编辑文本中设置字符限制。 like when i enter 100 it is entered or set max length 3. when i enter 30 it set max length 2 when i enter 4 then set max length 2 when i enter 2 then set max length 3
就像当我输入 100 时输入或设置最大长度 3。当我输入 30 时它设置最大长度 2 当我输入 4 然后设置最大长度 2 当我输入 2 然后设置最大长度 3
these all about number but in decimal(.) can't be enter first time or it can't be enter twice and after decimal it contain 2 value.这些都是关于数字的,但在十进制(。)中不能第一次输入,也不能输入两次,十进制后它包含 2 个值。 example 100.00 entered 200.00 entered 30.745 not entered(set max length 2 after decimal)
示例 100.00 输入 200.00 输入 30.745 未输入(设置最大长度为小数点后 2)
Edit text code编辑文本代码
ed_weight.onChange {
try {
if (it.startsWith(".")) {
ed_weight.limitLength(1)
} else if (it.toFloat() >= 30.00) {
if (it.contains(".")) {
var len = it.indexOf('.')
ed_weight.limitLength(len + 3)
} else {
ed_weight.limitLength(2)
}
} else if (it.toFloat() < 299.00) {
if (it.contains(".")) {
var len = it.indexOf('.')
ed_weight.limitLength(len + 3)
} else {
ed_weight.limitLength(3)
}
} else {
ed_weight.limitLength(3)
}
} catch (e: Exception) {
}
}
Extension file扩展文件
fun EditText.onChange(cb: (String) -> Unit) {
this.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) {
cb(s.toString())
}
})
}
fun EditText.limitLength(maxLength: Int) {
filters = arrayOf(InputFilter.LengthFilter(maxLength))
}
Try android:maxLength="3"试试 android:maxLength="3"
Ref: http://androidblogger.blogspot.com/2009/01/numeric-edittext-and-edittext-with-max.html参考: http : //androidblogger.blogspot.com/2009/01/numeric-edittext-and-edittext-with-max.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.