How to keep DropDownList of AutoCompleteTextView o

2019-05-06 23:41发布

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?

2条回答
三岁会撩人
2楼-- · 2019-05-07 00:34

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.

查看更多
叛逆
3楼-- · 2019-05-07 00:40

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);
}
查看更多
登录 后发表回答