Cambiando el valor Indirecta de TextInputLayout mientras el usuario escribe en ella

Desarrollador detenidos:

TextInputEditText

Aquí está mi TextInputEditTextinterior TextInputLayout. La pista se puede ver se encuentra en TextInputLayout.

El usuario debe escribir 255 caracteres de lo contrario el mensaje no es válido.

Quiero el dígito 255 a disminuir a medida que el usuario escribe y cuando llegó a 0, la pista debe estar en blanco.

Aquí está mi código:

private int charactersLeft = 256;se declara fuera de onCreatemétodo.

Y este código se encuentra dentro de onCreatemétodo:

final TextInputLayout postTextInputBase = findViewById(R.id.post_text_input_base);

final TextInputEditText postTextInput = findViewById(R.id.post_text_input);

postTextInput.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) {

        if (charactersLeft == 0) {
            postTextInputBase.setHint(null);
        } else {
            postTextInputBase.setHint((charactersLeft - 1) + " characters left");
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

También traté de conjunto charactersLefta characterLeft -= 1, pero, al javano voy a permitir que haga eso.

Entonces, ¿cómo puedo hacer esto?

Anisuzzaman Babla:

Prueba esto. Funciona con su código existente.

int charactersLeft = 256;
final TextInputLayout postTextInputBase = findViewById(R.id.post_text_input_base);
final TextInputEditText postTextInput = findViewById(R.id.post_text_input);

postTextInput.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 (s.length() == 0) {
                postTextInputBase.setHint(null);
            }else {
                postTextInputBase.setHint((charactersLeft - s.length()) + " characters left");
            }
        }
    });

y agregar android:maxLength="256"a suTextInputEditText

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=184509&siteId=1
Recomendado
Clasificación