Keyboard suggestions cause part of Android EditText.setError() message to not display

前端 未结 5 1437
迷失自我
迷失自我 2020-12-09 05:14

When I\'m using edittext.setError(\"enter a comment\") in android, it works fine until the keyboard suggestions come up and the error gets pushed above the

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 05:58

    setError
    Sets the right-hand compound drawable of the TextView to the "error" icon and sets an error message that will be displayed in a popup when the TextView has focus. The icon and error message will be reset to null when any key events cause changes to the TextView's text. If the error is null, the error message and icon will be cleared.

    So when the text is changed it should be gone. I don't know why this doesn't happen in your case.

    It should also be cleared when error message is null, so one trick could be:

    edittext = (EditText)findViewById(R.id.foo); // add below this line
    edittext.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){
            if(s != null && s.length() > 0 && edittext.getErrorMessage() != null) {
                edittext.setErrorMessage(null);
            }
        }
    }); 
    

提交回复
热议问题