EditText setError() with icon but without Popup message

后端 未结 6 1128
你的背包
你的背包 2020-11-27 03:11

I want to to have some validation for my EditText wherein I want to show \"\" icon (that comes when you put editText.setError(\"blah blah\")) but don\'t want th

6条回答
  •  旧巷少年郎
    2020-11-27 03:55

    To get only the error-icon without an error-message-popup only when setError("") is called (i.e. set an empty String as error-message) I use a custom EditText-class where I override setError(CharSequence, Drawable) like this:

    @Override
    public void setError(CharSequence error, Drawable icon) {
        if (error == null) {
            super.setError(null, icon);
            setCompoundDrawables(null, null, null, null);
        }
        else if (error.toString().equals("")) setCompoundDrawables(null, null, icon, null);
        else super.setError(error, icon);
    }
    

    Everything else stays the same:

    Use setError(null) to get neither the icon nor the message-popup.

    Use setError(errorMessage), where errorMessage is a String with length 1 at least, to get the icon and message-popup.

提交回复
热议问题