EditText no muestra la entrada actual (Android 4)

My Android application contains an Editar texto view where you can type some short messages (single line). Pressing the keyboard's LISTO key will append the message to a log view above (Vista de texto) and clear the input view.

Here's a snippet from my view xml:

<LinearLayout ...>
    <TextView
        android:id="@+id/logView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/inputView"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:imeOptions="actionDone"
        android:singleLine="true" />
</LinearLayout>

To handle the input and reset the view, I use the OnEditorActionListener.

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    ...
    String input = mInputView.getText().toString();
    mInputView.setText(""); // clear the input view
    ...
}

El problema

I did not experience any problems on Android 1.6 - 3. But starting with IceCreamSandwich (>= Android 4) there's a weird bug which occurs intermittently (in most cases after ~10-30 inputs).

When you type some text, the input view remains blank. The cursor still blinks on position 0, no text is shown. Though a click on LISTO adds the (invisible) text to the log view above and the text can be read. Also hiding the keyboard makes the text in the EditText view visible.

solución

As stated in the accepted answer this is a (not so much) known bug of the Android OS. The simple solution is to clear the EditText view in a different way:

TextKeyListener.clear(mInputView.getText());

preguntado el 30 de enero de 12 a las 19:01

3 Respuestas

I had exactly the same issue, even on lower API levels. There's a bug when using:

editText.setText("");

many times to empty an EditText. Here's a workaround that helped:

TextKeyListener.clear(editText.getText());

You can read about this bug on the Google Code site: http://code.google.com/p/android/issues/detail?id=17508

¡Espero eso ayude!

Respondido 24 Feb 12, 04:02

try setting an OnClickListener on your done button. Have the onClick(View v) se parece a esto:

@Override
public void onClick(View v){
    kontextTV1.setText(editText1.getText.toString());
}

If you pull the text when the user hits the done button, you won't have to use a watcher class. This should also work on all versions of android. (Get/Set on edittext and textview aren't likely to change). That will handle a

If you want to handle the 'done' button on they keyboard, try:

editText1.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(final View v, final int keyCode, final KeyEvent event) {
    if (KeyEvent.KEYCODE_ENTER == keyCode) {
        //...
    }
}

Respondido 19 Feb 12, 20:02

How can I add an OnClickListener on the DONE button of the virtual keyboard? I don't think that this is possible. If, though, does it also work on hardware keyboards and third-party virtual keyboard, e.g. HTC Sense? - ottel142

I must have misread your question, edited response to show how to do this. - edthethird

Your solution works, but it does not fix the bug I've got using an OnEditorActionListener. After a series of inputs, the EditText does not show the input :( - ottel142

Why not use afterTextChanged instead of editorActionListener?

Respondido 18 Feb 12, 02:02

The user submits his input with the DONE key, this is very important! Also an empty input has to be submittable. So I can't use a TextChangedListener, right? - ottel142

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.