繁体   English   中英

Android系统。 如何捕捉隐藏键盘的瞬间?

[英]Android. How to catch the moment when keyboard gets hidden?

我正在尝试捕获从屏幕上删除键盘的事件,并且我正在使用以下代码:

    searchTextField.setOnEditorActionListener(new OnEditorActionListener()
    {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
        {

            System.out.println("ACTION ID " + actionId);

            if(actionId == EditorInfo.IME_ACTION_DONE)
            {
                System.out.println("ACTION DONE!!!!!!!!!!");
                return true;
            }

            return false;
        }

   searchTextField.setOnFocusChangeListener( new View.OnFocusChangeListener()
    {
        public void onFocusChange(View v, boolean hasFocus) 
        {
              if (hasFocus)
                  System.out.println("HAS FOCUS");
              else
                  System.out.println("FOCUS LOST");
        }
    });

但不幸的是,它不起作用。 onEditorAction从未调用过,无论我是否开始编辑或完成。 关于onFocusChange方法,只有在键盘上升时才第一次调用它。 当键盘下降或第二次上升时,不会被调用。 谁能解释我在做什么错?

我在活动rootView上使用GlobalLayoutListener来检查键盘是隐藏还是可见:

其工作方式如下:

final View activityRootView = findViewById(R.id.activityRoot);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
            if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                ... do something here
            }
         }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM