问题
I am working on an android application. In an activity at the bottom of the page, I am showing a popupWindow and replaced the keyboard with this popupwindow. This popupWindow is having a searchview inside it, so when searchview will be in focus the keyboard will be shown and popupWindow will be slide up. Now when I press Back Button (Phone's Back Button) then keyboard as well as popupwindow both are getting closed. So, I want to close only keyboard here and popupwindow should be slide down after keyboard will be closed.
I tried the below things :
setBackgroundDrawable(new BitmapDrawable());
poupupWindow.setFocusable(true);
Applied KeyListner also on popupWindow, but it's not working.
popupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_DOWN) {
return true;
} return false;
}
});
Please help me if anyone have idea about this. Thanks a ton in advanced :)
回答1:
add code to hide keyboard inside key listener using onKeyDown() mehod.
boolean isKeyboardHidden=false;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BACK;
if(!isKeyboardHidden && popupview.isFocused())
{
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
isKeyboardHidden=true;
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
来源:https://stackoverflow.com/questions/39508270/keyboard-and-popup-window-closes-on-backpress