我已經實現一個簡單的驗證用於文本編輯.setError(),使用此代碼:機器人:驗證的EditText與TextWatcher和
title = (EditText) findViewById(R.id.title);
title.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if (title.getText().length() < 1) {
title.setError("Title is required");
} else {
title.setError(null);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
});
的funcion檢查,如果有插入上的textchange和一切任何文本完美的作品,除非我把光標放在已經是空的標題字段中,然後再按一次刪除。錯誤信息被重新設置,並且不會調用textwatcher,因爲沒有文本更改。我怎麼能在這種情況下顯示錯誤信息?
正在調用文本監視器。但是,在已經是空的文本字段中,如果您點擊刪除鍵,請注意沒有文本更改,因此afterTextChanged不會被調用。 –
找到解決方案!請參閱下面的答案。 –