How to detect Back Key in my CustomView

I want to detect the Back Key event in my CustomView (e.g. EditText). In many cases, this was achieved by overriding onKeyDown()or dispatchKeyEvent()assuming that my CustomView gets focus.

CustomView.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if( keyCode == KeyEvent.KEYCODE_BACK) {
        ..... 
        return true;
    }else{
        return super.onKeyDown(keyCode, event);
    }
}

However, if an Activity, including CustomView, also overrides onKeyDown()or dispatchKeyEvent(), it may not work much. That is, the activity received a Back-KeyEvent before the CustomView has.

I prefer to catch a Back-KeyEvent before an action.

please tell me some ideas about this problem. Thank.

+5
source share
3 answers

You need to implement this in order to capture the BACK button before sending to IME:

http://developer.android.com/reference/android/view/View.html#onKeyPreIme(int,android.view.KeyEvent)

+2

onKeyDown false. .

, true. , false.

0

You can try using

setFocusableInTouchMode(true)
setFocusable(true)
requestFocus()

in your user view

0
source

All Articles