Android Editext having issue with backspace key
Asked Answered
E

3

7

I have an AlertDialog in which I am setting a XML as its view. In that xml layout I have an EditText. But after entering data in EditText, if I am trying to delete using backspace, the characters are not getting deleted (its like backspace is not working).

Am I missing something? I searched but did not get any proper solution except adding keylistener. I think it should work simple?

Anyone there to help me.

Here is my EditText

<EditText
        android:id="@+id/TextBox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text">
        <requestFocus />
</EditText>

Dialog ceation code:

hintDialog = new AlertDialog.Builder(activity)
    .setTitle("Enter Your Hint:")
    .setView(hintDialogView).create();
    hintDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
      @Override
      public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK)
        hintDialog.dismiss();
        return true;
      }
    });
Elyn answered 30/7, 2012 at 6:44 Comment(1)
please paste the code of AlertDialog creation portionZarzuela
D
17

Do you have any onKeyListeners set? This can be the cause of the problem.

try this:

 hintDialog = new AlertDialog.Builder(activity)
.setTitle("Enter Your Hint:")
.setView(hintDialogView).create();
hintDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
  @Override
  public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK)
    hintDialog.dismiss();
    return true;
  }
  return false;
});

(adding the return false;)

Danforth answered 30/7, 2012 at 7:10 Comment(1)
You are my time saver... More than 3 hours searching for this exact fix..Chamonix
R
1

Do you have KeyEvent.KEYCODE_BACK listener? Called to process key events. You can override this to intercept all key events before they are dispatched to the window. Be sure to call this implementation for key events that should be handled normally. when you override dispatchKeyEvent method,u must call super.dispatchKeyEvent(event) when return.

Recaption answered 30/7, 2012 at 7:7 Comment(0)
J
0

Should not KEYCODE_BACK but KEYCODE_DEL instead. It works for sure in your case.

Jaella answered 20/2, 2013 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.