[英]How to hide EditText error after typing on textfield?
在這里,我首先嘗試使用空白值登錄,並成功顯示了錯誤。 現在,當我在電子郵件文本字段中鍵入一個值時,我想隱藏錯誤“需要電子郵件”。 當我在電子郵件文本字段中鍵入第一個鍵時,我想刪除錯誤消息“需要電子郵件”
。 如何在此代碼中實現這樣的功能?
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) {
}
});
您可以像控制錯誤的可見性。 我假設您可以獲得對錯誤標簽的引用,並將其存儲在名為errorLabel
的變量中
@Override
public void onClick(View v) {
errorLabel.setVisibility( validateEmail() && validatePassword() );
}
您必須添加文本查看器才能編輯文本。 然后隱藏“ Required
錯誤。 查看以下示例,以更好地理解。
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.AppCompat">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="12dp">
</android.support.design.widget.TextInputLayout>
然后在你的活動/片段中
private EditText passwordEditText;
private TextInputPassword textInputPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Initializing views */
passwordEditText = (EditText) findViewById(R.id.password);
/* Set Text Watcher listener */
passwordEditText.addTextChangedListener(passwordWatcher);
textInputPassword = (TextInputLayout)
findViewById(R.id.text_input_password);
}
private final TextWatcher passwordWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
textInputPassword.setErrorEnabled(true);
textInputPassword.setError("Password Required");
} else{
textInputPassword.setErrorEnabled(false);
textView.setText("You have entered : " + passwordEditText.getText());
}
}
};
只是將錯誤文本設置為空字符串
editText.setError("", null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.