在文本字段上键入后如何隐藏EditText错误?

时间:2019-06-19 16:53:47

标签: java android xml

login

在这里,我首先尝试使用空白值登录,它成功显示了错误。现在,当我在电子邮件文本字段中键入一个值时,我想隐藏错误“需要电子邮件”。当我在电子邮件文本字段中键入第一个键时,我想删除错误消息“需要电子邮件”
。如何在此代码中实现这样的功能?

  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;
        }

    }

4 个答案:

答案 0 :(得分:1)

您可以使用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) {  
                }  
            });  

答案 1 :(得分:0)

您可以像这样控制错误的可见性。我假设您可以获取对错误标签的引用,并将其存储在名为errorLabel

的变量中
@Override
public void onClick(View v) {
  errorLabel.setVisibility( validateEmail() && validatePassword() );
}

答案 2 :(得分:0)

您必须添加文本查看器才能编辑文本。然后隐藏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());
                    }
                }
            };

答案 3 :(得分:0)

只需将错误文本设置为空字符串

editText.setError("", null);