How to keep DropDownList of AutoCompleteTextView o

2019-05-06 23:59发布

问题:

i am using AutoCompleteTextView in my Activity and i need it's DropDownList to be shown all the time (it's the only View in Window), even after Back key press. I need to dismiss soft keyboard instead.

I tried to override Activity's onBackPressed method, but it's not used at all, so BackPressed event is being handled somewhere "higher". So i tried to find out where, but AutoCompleteTextView has no onBackPressed method defined.

Any advices?

回答1:

You can create your custom AutoCompleteTextView and Override the method onKeyPreIme(int keyCode, KeyEvent event)

I also realized that this method is called 2 times, I'm running my code only in the second time. Here is the example:

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
        //add your code here
        return true;
    }
    return super.onKeyPreIme(keyCode, event);
}


回答2:

You may try this

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
            //Your back key press logic
    }
    return true;
}

Remember to return true to prevent this event from being propagated further, or false to indicate that you have not handled this event and it should continue to be propagated.