如何将设置错误隐藏到编辑文本Android中

时间:2016-10-27 12:20:55

标签: android android-edittext

我有两个EditText,当编辑文本为空时我设置了错误,但问题是当EditText取值时,错误仍然显示,如下所示:

enter image description here

这是我的代码:

 String username=  edit_text_username.getText().toString();
    String password = edit_text_password.getText().toString();
    if (username.equals("")) {
        progressDialogLogin.dismiss();
        layoutUsername.setError(" required!");
        Layoutpassword.requestFocus();
    }
    else if (password.equals("")) {
        progressDialogLogin.dismiss();
        Layoutpassword.setError(" required!");
    }

        else {

任何想法?

4 个答案:

答案 0 :(得分:2)

您可以添加TextChangedListener以检查是否为空/非空。例如:

edit_text_username.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() > 0){
                layoutUsername.setError("");
            } else {
                layoutUsername.setError("user name required");
            }
        }
    });

如果您想要在点击登录按钮时进行验证,可以像这样使用:

String username=  edit_text_username.getText().toString();
String password = edit_text_password.getText().toString();
loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.equals("")) {
                    progressDialogLogin.dismiss();
                    layoutUsername.setError(" required!");
                    Layoutpassword.requestFocus();
                } else {
                    layoutUsername.setError("");
                }
                if (password.equals("")) {
                    progressDialogLogin.dismiss();
                    Layoutpassword.setError(" required!");
                } else {
                    Layoutpassword.setError("");
                }
            }
        });

答案 1 :(得分:0)

使用android.support.design.widget.TextInputLayout在Edittext中显示错误:

.xml 代码:

<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint" />

<强>的strings.xml

<string name="err_msg">required!</string>

Java 代码:

TextInputLayout inputLayout = (TextInputLayout) findViewById(R.id.input_layout);
EditText input = (EditText) findViewById(R.id.input);


 // to show error use this code 
 inputLayout .setError(getString(R.string.err_msg));

答案 2 :(得分:0)

试试这个,

edtUserName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (edtUserName.getText().toString().trim().length() > 0) {
                    usernameLayout.setError(null);

                }
            }
        });

希望这会对你有所帮助

答案 3 :(得分:0)

更有效的方法是:

  private boolean validatePassword() {
        if (pass.getText().toString().trim().isEmpty()) {
            pass.setError(getString(R.string.err_msg_pass));
            requestFocus(text_pass);
            return false;
        }else if(pass.getText().toString().length()<8) {
            pass.setError(getString(R.string.err_msg_pass_length));
            requestFocus(text_pass);
            return false;
        }else {
            text_pass.setErrorEnabled(false);
        }
        return true;
    }