Android:如何判断软键盘是否显示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3568919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:47:02  来源:igfitidea点击:

Android: how can i tell if the soft keyboard is showing or not?

android

提问by james

Heres the dilemma: I am showing a screen with 3 input fields and 2 buttons inside of a tab(there are 3 tabs total, and they are on the bottom of the screen). the 2 buttons are set to the bottom left and right of the screen, right above the tabs. when i click on an input field, the tabs and buttons are all pushed up on top of the keyboard.

这就是困境:我正在显示一个屏幕,其中包含 3 个输入字段和一个选项卡内的 2 个按钮(总共有 3 个选项卡,它们位于屏幕底部)。2 个按钮设置在屏幕的左下方和右下方,选项卡的正上方。当我单击输入字段时,选项卡和按钮都被推到键盘顶部。

i desire to only push the buttons up, and leave the tabs where they originally are, on the bottom. i am thinking of setting the visibility of the tabs to GONE once i determine that the soft keyboard is showing, and visibility to VISIBLE once the soft keyboard is gone.

我只想将按钮向上推,并将标签留在底部的原始位置。一旦我确定软键盘正在显示,我正在考虑将选项卡的可见性设置为 GONE,并且在软键盘消失后将可见性设置为 VISIBLE。

is there some kind of listener for the soft keyboard, or maybe the input field? maybe some tricky use of OnFocusChangeListenerfor the edit text? How can i determine whether the keyboard is visible or not?

软键盘或输入字段是否有某种侦听器?也许一些棘手的OnFocusChangeListener用于编辑文本?如何确定键盘是否可见?

采纳答案by pjv

Determining if the keyboard is showing is not possible apparently.

确定键盘是否显示显然是不可能的。

You might want to disable it alltogether with the windowSoftInputModexml tag in your manifest: http://developer.android.com/reference/android/R.attr.html#windowSoftInputMode. Or you can have a look on how to remove focus to hide the keyboard: Hide soft keyboard on activity without any keyboard operations.

您可能希望将其与windowSoftInputMode清单中的xml 标记一起禁用:http: //developer.android.com/reference/android/R.attr.html#windowSoftInputMode。或者您可以查看如何移除焦点以隐藏键盘:Hide soft keyboard on activity without any keyboard operations

Neither will exactly solve your problem. I remember reading a blogpost strongly advising not to use tabs at the bottom, rather than the top of the screen, for UI clarity reasons. I recommend you to follow up on that.

两者都不能完全解决您的问题。我记得读过一篇博文,强烈建议不要在底部使用标签,而不是在屏幕顶部,因为 UI 清晰的原因。我建议你跟进。

回答by android developer

As far as I know, you can't.

据我所知,你不能。

However, you can monitor size changes of your layout, and since keyboard showing up is the main cause for the resizes, you might be able to assume that the keyboard is shown or not.

但是,您可以监控布局的大小变化,并且由于键盘显示是调整大小的主要原因,您可以假设键盘是否显示。

Here's a sample code for monitoring the size-changes of a layout. Just use this layout as the parent of your original layout, and use its listener. If the height has decreased, you can assume the keyboard is shown, and if it was increased, you can assume it got closed.

这是用于监视布局大小变化的示例代码。只需将此布局用作原始布局的父级,并使用其侦听器即可。如果高度降低,您可以假设显示了键盘,如果增加了,您可以假设它已关闭。

public class LayoutSizeChangedSensorFrameLayout extends FrameLayout {
    public enum SizeChange {
        HEIGHT_INCREASED, HEIGHT_DECREASED, WIDTH_INCREASED, WIDTH_DECREASED
    }

    public interface OnLayoutSizeChangedListener {
        void onSizeChanged(EnumSet<SizeChange> direction);
    }

    private OnLayoutSizeChangedListener mLayoutSizeChangeListener;

    public LayoutSizeChangedSensorFrameLayout(final Context context) {
        super(context);
    }

    public LayoutSizeChangedSensorFrameLayout(final Context context, final AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public LayoutSizeChangedSensorFrameLayout(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

  @Override
  protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
      super.onSizeChanged(w, h, oldw, oldh);
      if (mLayoutSizeChangeListener != null) {
          final EnumSet<SizeChange> result = EnumSet.noneOf(SizeChange.class);
          if (oldh > h)
              result.add(SizeChange.HEIGHT_DECREASED);
          else if (oldh < h)
              result.add(SizeChange.HEIGHT_INCREASED);
          if (oldw > w)
              result.add(SizeChange.WIDTH_DECREASED);
          else if (oldw < w)
              result.add(SizeChange.WIDTH_INCREASED);
          if (!result.isEmpty())
              mLayoutSizeChangeListener.onSizeChanged(result);
      }
  }

    public void setOnLayoutSizeChangedListener(final OnLayoutSizeChangedListener layoutSizeChangeListener) {
        this.mLayoutSizeChangeListener = layoutSizeChangeListener;
    }

    public OnLayoutSizeChangedListener getOnLayoutSizeChangeListener() {
        return mLayoutSizeChangeListener;
    }
}

回答by Shivam Yadav

Working solution for me -

为我工作的解决方案 -

ViewTreeObserver treeObserver = getViewTreeObserver();
        if (treeObserver != null)
            treeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int pHeight = getResources().getDisplayMetrics().heightPixels;

                Rect visRect = new Rect();
                viewGroup.getWindowVisibleDisplayFrame(visRect);

                boolean keyboardVisible;
                int keyboardHeight= pHeight-currentHeight;
                if (keyboardHeight > (MIN_KEYBOARD_HEIGHT*pHeight))
                {
                    TGVLog.d(debugTag, "keyboardVisible-- "+true);
                    keyboardVisible = true;
                }
                else
                {
                    TGVLog.d(debugTag, "keyboardVisible-- "+false);
                    keyboardVisible = false;
                }
                }
            });

回答by HaMMeReD

This might help http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html

这可能有助于 http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html

https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/latin

https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/latin

The keyboard code is here, I grepped through it a little but give up, I don't see any Broadcasts being sent that would be of any use, at least in here. Maybe you can go find lower level code in the repo and find a useful Intent being sent. The first linke might tell you when it becomes visible, but I'm not sure how to tell when it becomes invisible.

键盘代码在这里,我稍微浏览了一下,但放弃了,我没有看到发送任何有用的广播,至少在这里是这样。也许您可以在 repo 中找到较低级别的代码并找到发送的有用 Intent。第一个链接可能会告诉您它何时变得可见,但我不确定如何判断它何时变得不可见。

回答by rahul

In my last application I had some issues in displaying the soft keyboard, then I have used the following code in my Manifest file:

在我的上一个应用程序中,我在显示软键盘时遇到了一些问题,然后我在清单文件中使用了以下代码:

<activity android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" android:name=".rate.Calculator"/>

<activity android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" android:name=".rate.Calculator"/>