【发布时间】:2019-08-02 10:30:18
【问题描述】:
我对 editText 元素进行了一些简单的正则表达式验证。我遇到的问题是,如果验证失败(即使用户仍在键入),错误会立即显示,这不是很好的 UX。这是当前的代码。
TextWatcher tw = new TextWatcher() {
public void afterTextChanged(Editable s) {
String currentTime = t_timeEditText.getText().toString();
if (!validTimepattern.matcher(currentTime).matches()){
timeEditText.setError("Not a valid time");
}
}
}
我认为最好的解决方案是等到焦点从 editText 元素移开后再运行上述验证。或者,我们可以在运行验证之前等待自上次输入以来的 X 毫秒,或者只是在其中添加一些讨厌的硬编码延迟。
有什么建议吗?
【问题讨论】:
-
I think the best solution would be to wait until the focus moves off那么你应该使用焦点改变监听器:stackoverflow.com/questions/10625775/android-on-focus-change -
关于为什么这被否决的任何反馈?我不介意,我只是想了解一下,以便改进以后的问题。
标签: java android error-handling android-edittext