键盘和弹出窗口在BackPress上关闭 [英] Keyboard and Popup window closes on BackPress

查看:157
本文介绍了键盘和弹出窗口在BackPress上关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android应用程序.在页面底部的活动中,我正在显示一个popupWindow并用此popupwindow替换了键盘.这个popupWindow内部有一个searchview,因此当searchview处于焦点时,将显示键盘,并且popupWindow将向上滑动.现在,当我按下后退"按钮(电话的后退"按钮)时,键盘以及弹出窗口都将关闭. 因此,我只想在这里关闭键盘,然后在关闭键盘后将popupwindow向下滑动.

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.

我尝试了以下操作:

setBackgroundDrawable(new BitmapDrawable());
poupupWindow.setFocusable(true);

也在popupWindow上应用了KeyListner,但是它不起作用.

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 :)

推荐答案

添加代码以使用onKeyDown()方法将键盘隐藏在按键监听器中.

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);
        }

这篇关于键盘和弹出窗口在BackPress上关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆