Desarrollador detenidos:
Aquí está mi TextInputEditText
interior 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 onCreate
método.
Y este código se encuentra dentro de onCreate
mé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 charactersLeft
a characterLeft -= 1
, pero, al java
no 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