我有editext的通用textwatcher。我使用setError()对editext进行了验证,如下所示
private class GenericTextWatcher implements TextWatcher {
private View view;
private GenericTextWatcher(View view) {
this.view = view;
}
// @Override
public void afterTextChanged(Editable editable) {
switch (view.getId()) {
case R.id.autocompletetextview:
isAlphaNumeric = validateEditText("^[a-zA-Z0-9 ,.-]*$", editLocation.getText().toString());
if (!isAlphaNumeric) {
// editLocation.setError(getErrorMsg("Only , . and - are allowed"));
editLocation.setError(getText(R.string.invalid_loc_address));
} else {
}
}
}
// @Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
}
![enter image description here][1]
// @Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {
}
}
我正在使用的验证与edittext重叠,并且editext中的值不可见。是否可以转移错误消息? 请为问题提出合适的解决方案
答案 0 :(得分:0)
将Textview组件放入xml文件中。给id tvError
。
TextView tvError = (TextView)findViewById(R.id.tvError);
private class GenericTextWatcher implements TextWatcher {
private View view;
private GenericTextWatcher(View view) {
this.view = view;
}
// @Override
public void afterTextChanged(Editable editable) {
switch (view.getId()) {
case R.id.autocompletetextview:
isAlphaNumeric = validateEditText("^[a-zA-Z0-9 ,.-]*$", editLocation.getText().toString());
if (!isAlphaNumeric) {
tvError.setText(getText(R.string.invalid_loc_address));
} else {
}
}
}
// @Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
}
// @Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {
}
}