Android:如何验证EditText输入?我需要在一系列EditTexts上进行表单输入验证。我使用OnFocusChangeListeners在用户输入每个之后触发验证,但这不符合上一个EditText的需要。如果在键入最终的EditText时单击“完成”按钮,则InputMethod将断开连接,但技术上焦点永远不会丢失在EditText上(因此永远不会发生验证)。什么是最好的解决方案?当InputMethod从每个EditText解除绑定而不是焦点更改时,我应该监视吗?如果是这样,怎么样?
3 回答
![?](http://img1.sycdn.imooc.com/54584ef20001deba02200220-100-100.jpg)
鸿蒙传说
TA贡献1865条经验 获得超7个赞
TextWatcher对我的口味有点冗长,所以我做了一些容易吞下的东西:
public abstract class TextValidator implements TextWatcher { private final TextView textView; public TextValidator(TextView textView) { this.textView = textView; } public abstract void validate(TextView textView, String text); @Override final public void afterTextChanged(Editable s) { String text = textView.getText().toString(); validate(textView, text); } @Override final public void beforeTextChanged(CharSequence s, int start, int count, int after) { /* Don't care */ } @Override final public void onTextChanged(CharSequence s, int start, int before, int count) { /* Don't care */ }}
只需使用它:
editText.addTextChangedListener(new TextValidator(editText) { @Override public void validate(TextView textView, String text) { /* Validation code here */ }});
- 3 回答
- 0 关注
- 798 浏览
添加回答
举报
0/150
提交
取消