How to keep DropDownList of AutoCompleteTextView opened after pressing the Back key?

北慕城南 提交于 2019-12-10 07:58: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.



来源:https://stackoverflow.com/questions/9722278/how-to-keep-dropdownlist-of-autocompletetextview-opened-after-pressing-the-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!