saturngod
saturngod

Reputation: 24949

Delete key is not working

I added setOnKeyListener for Enter keyevent. However, after I adding setOnKeyListener , delete (backspace) key is not working. When I removed the setOnKeyListener , delete key is working fine.

How to fix the delete key working well ?

final EditText edittext = (EditText) findViewById(R.id.editText1);

        edittext.setOnKeyListener(new OnKeyListener() {


            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent event) {
                // TODO Auto-generated method stub
                 if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {

                     InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        in.hideSoftInputFromWindow(edittext
                                .getApplicationWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);

                     applySearch();


                 }
                return true;
            }


        });

Upvotes: 4

Views: 4562

Answers (2)

aldo.roman.nurena
aldo.roman.nurena

Reputation: 1332

If you return True, you are handling all the keys. Try this:

if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
    // something here
    return true;
}

// otherwhise
return false;

Android: Problem with overriding onKeyListener for a Button

Upvotes: 4

Andrey Ermakov
Andrey Ermakov

Reputation: 3338

According to the documentation onKey returns True if the listener has consumed the event, false otherwise. In your case:

@Override
public boolean onKey(View arg0, int arg1, KeyEvent event) {
    ...
    return true; // Try to return false instead
}

When your method returns true keys aren't processed and backspace doesn't work.

Upvotes: 3

Related Questions