Keyboard and Popup window closes on BackPress

扶醉桌前 提交于 2019-12-25 08:03:04

问题


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

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