开发者

EditText setError() with no message just the icon

Currently开发者_如何学C I have a really simple code that validates EditText fields and when the user hits the button at the end it checks them. It will put errors in all the fields with this:

if (!emailmatcher.matches()) {
    email.setError("Invalid Email");  
}
if (!zipmatcher.matches()) {
    zipcode.setError("Invalid ZipCode");                     
}

My problem that is the keyboard will popup and will move the error bubble to a random place. I was hoping to not have a error bubbles with a message but keep the error icons in invalid EditText fields. I tried inputting setError(null) but that doesn't work. Any Ideas?


Your code perfect to show icon only.

As EditText is show anything related to when it has focused. So just change your code to like that...

if (!emailmatcher.matches()) {
    email.requestFocus();
    email.setError("Invalid Email");  
}
if (!zipmatcher.matches()) {
    zipcode.requestFocus();
    zipcode.setError("Invalid ZipCode");                     
}


You can use

setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.my_error_icon, 0);

to set the error icon to the right of your text input. You may also have to register

View.OnKeyListener to reset the error icon on user input:

EditText myEdit = (EditText)findViewById(R.id.my_edit);

if (TextUtils.isEmpty(myEdit.getText().toString())) {
    // set the warning
    myEdit.requestFocus();

    // show error icon
    myEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error_sign, 0);

    // remove the error icon on key press
    myEdit.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            EditText eText = (EditText)v;

            // remove error sign
            eText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

            // remove this key handler
            eText.setOnKeyListener(null);

            return false;
        }
    });
}


This works for me. Now getting error messages pop up open.

public boolean isValid() {
        String email = ediTextEmail.getText().toString();
        String password = ediTextPassword.getText().toString();
        if (TextUtils.isEmpty(email)) {
            ediTextEmail.requestFocus();
            ediTextEmail.setError("Email cannot be blank");
            return false;
        }

        if (!isEmailValid(email)) {
            ediTextEmail.requestFocus();
            ediTextEmail.setError("Enter a valid email id");
            return false;
        }

        if (TextUtils.isEmpty(password)) {
            ediTextPassword.requestFocus();
            ediTextPassword.setError("Password cannot be blank");
            return false;
        }

        mEmail = email;
        mPassword = password;
        return true;
    }

Pop opened even if my focus is on password editText, it will be redirected to email editText.


simply create your own edittext and override setError

    public class MyEditText extends EditText {

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void setError(CharSequence error, Drawable icon) {
    setCompoundDrawables(null, null, icon, null);
 }
}  

and then use wherever you want et.setError("", drawableToShow);


You can use my custom solution for popups. In this case popup is independent from EditText focus state. Moreover it is usable with multiple EditTexts. https://plus.google.com/+AlexanderZaitsevDev/posts/W9XcZNZrsMB

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜