gpt4 book ai didi

android - Jetpack Compose TextField InputFilter 仅具有货币正则表达式输入

转载 作者:行者123 更新时间:2023-12-04 08:17:42 39 4
gpt4 key购买 nike

是否有可能有 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

}

}

最佳答案

我的建议是在插入的值无效时显示错误。例如:

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")
}

关于android - Jetpack Compose TextField InputFilter 仅具有货币正则表达式输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65641875/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com