code

EditText setError 메시지가 입력 후 지워지지 않음

codestyles 2020. 10. 28. 08:07
반응형

EditText setError 메시지가 입력 후 지워지지 않음


좋아, 나는 EditText 필드와 버튼이 있는데, 누르면 AsyncTask가 트리거됩니다.

EditText playerName = (EditText)findViewById(R.id.playerEditText);

if(playerName.getText().toString().length() == 0 )
    playerName.setError("Player name is required!");
else {
    // do async task
}

문제는 검색을 위해 유효한 텍스트를 입력 한 후에도 오류 메시지가 계속 표시되는 것 같습니다. EditText가 비어 있지 않으면 오류를 제거하는 방법이 있습니까?


else 괄호에을 넣으면 playerName.setError(null)오류가 지워집니다.


API 문서 : "키 이벤트로 인해 TextView의 텍스트가 변경되면 아이콘과 오류 메시지가 null로 재설정됩니다." 그렇지는 않지만-따라서 우리는 이것을 버그로 간주 할 수 있습니다.

textNoSuggestions, textEmailAddress, textPassword와 같은 inputType을 사용하면 문자를 입력하면 오류가 설정되지 않습니다. 거의 문서화되었지만 정확히는 아닙니다. 문자를 삭제해도 오류가 계속 발생합니다. addTextChangedListener 및 setError (null)을 사용하는 간단한 해결 방법으로 약속 된 동작을 얻을 수 있습니다.

Android 4.2에서 아이콘 손실에 대한 게시물이 있습니다. 따라서주의해서 사용하십시오.


이 리스너를 시도하십시오.

      playerName.addTextChangedListener(new TextWatcher()
                {
                    public void afterTextChanged(Editable edt){
                        if( playerName.getText().length()>0)
                        {
                             playerName.setError(null);
                        }
                    }

오류 메시지를 숨기려면 편집 상자에 onclicklistener를 적용한 다음

editTextName.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            editTextName.setError(Null)
        }
    });

아래 코드는 나를 위해 일했습니다.

@OnTextChanged(
            value = R.id.editTextName,
            callback = OnTextChanged.Callback.TEXT_CHANGED)
    public void afterInput(CharSequence sequence) {
        editTextName.setError(null);
        editTextName.setErrorEnabled(false);
    }

'

editTextName.setError(null) 오류 메시지를 지 웁니다.

editTextName.setErrorEnabled(false) 추가 패딩을 제거합니다.


EditText 및 onError에 TextWatcher를 추가하고, et.setError(errorMessage)그렇지 않으면 아래와 같이 오류 메시지와 오류 아이콘을 제거 할 수 있습니다.

// to remove the error message in your EditText
et.setError(null);

// to remove the error icon from EditText.
et.setCompoundDrawables(null, null, null, null);

참고URL : https://stackoverflow.com/questions/11640772/edittext-seterror-message-does-not-clear-after-input

반응형