是否有可能有 InputFilters 的列表,例如只让输入,例如
$1,01
或 $100,95
已完成
editText.filters = arrayOf(CurrencyFormatInputFilter(), InputFilter.LengthFilter(8))
class CurrencyFormatInputFilter : InputFilter {
private val pattern = Pattern.compile(CURRENCY_INPUT_REGEX)
override fun filter(
source: CharSequence,
start: Int,
end: Int,
dest: Spanned,
dstart: Int,
dend: Int
): CharSequence? {
val result = (dest.subSequence(0, dstart).toString()
+ source.toString()
+ dest.subSequence(dend, dest.length))
val matcher = pattern.matcher(result)
return if (!matcher.matches()) dest.subSequence(dstart, dend) else null
}
}
答案 0 :(得分:0)
我的建议是在插入的值无效时显示错误。例如:
fun isValidEmail(emailStr: String?) =
Pattern
.compile(
"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$",
Pattern.CASE_INSENSITIVE
).matcher(emailStr).find()
...
var emailText by remember { mutableStateOf("") }
var showError by remember { mutableStateOf(false) }
TextField(
value = emailText,
onValueChange = {
emailText = it
showError = !isValidEmail(it)
},
isErrorValue = showError,
label = { Text(text = "Email") },
modifier = Modifier.fillMaxWidth()
)
if (showError) {
Text("Email is invalid")
}