在这里,我第一次尝试使用空白值登录,它成功显示了错误。现在,当我在电子邮件文本字段中输入值时,我希望隐藏错误“需要电子邮件”。当我在电子邮件文本字段中键入第一个键时,我想删除错误消息“需要电子邮件”
。我怎样才能在这段代码中实现这样的功能?
textInputEmail = (TextInputLayout) findViewById(R.id.text_input_email);
textInputPassword = (TextInputLayout) findViewById(R.id.text_input_password);
login =(Button) findViewById(R.id.loginBtn);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!ValidateEmail() || !ValidatePassword())
{
return;
}
String inputs = "Email : "+ textInputEmail.getEditText().getText().toString().trim();
inputs +="\n";
inputs +="Password : "+ textInputPassword.getEditText().getText();
Toast.makeText(MainActivity.this,inputs, Toast.LENGTH_SHORT).show();
}
});
}
private Boolean ValidateEmail() {
String emailPattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
String emailInput = textInputEmail.getEditText().getText().toString().trim();
if (emailInput.isEmpty()) {
textInputEmail.setError("Email required");
return false;
} else if (!emailInput.matches(emailPattern)) {
textInputEmail.setError("Invalid Email");
return false;
} else {
textInputEmail.setError(null);
textInputEmail.setErrorEnabled(false);
return true;
}
}
最佳答案
您可以使用 TextWatcher 在文本更改时消除错误
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
textInputEmail.setError(null);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
关于java - 在文本字段中输入后如何隐藏 EditText 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56672565/