EditText does not show current input (Android 4)

我怕爱的太早我们不能终老 提交于 2019-12-07 05:13:27

问题


My Android application contains an EditText view where you can type some short messages (single line). Pressing the keyboard's DONE key will append the message to a log view above (TextView) 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
    ...
}

The problem

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 DONE 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.

Solution

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());

回答1:


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

Hope it helps!




回答2:


try setting an OnClickListener on your done button. Have the onClick(View v) look like this:

@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) {
        //...
    }
}



回答3:


Why not use afterTextChanged instead of editorActionListener?



来源:https://stackoverflow.com/questions/9069803/edittext-does-not-show-current-input-android-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!