java - 检测键盘何时在 Web View 中处于 Activity 状态

标签 java android webview keyboard android-webview

我还没有找到在堆栈上解决这个问题的任何方法。

我的应用程序中有一个 WebView 。我希望能够检测键盘何时处于 Activity 状态以及何时处于非 Activity 状态。当它们在 webview 中发生时,它似乎无法检测到这些变化。

我想对这些不同的状态执行操作。在 iOS 上,当键盘处于 Activity 状态时,监听器非常简单。引用 UIKeyboardWillShow/Hide。

Android 中是否有任何功能与这些观察者在 Android 中的功能相同?

希望这个问题得到足够的解释。

最佳答案

所以我为此苦苦挣扎了一个星期左右,并找到了很多 Material 这是我的解决方案,我真的希望它对你有用。

所以在我的例子中,我有一个 Activity,我调用了一个包含 Webview 的 fragment ,所以它比我想象的要复杂得多。

基本上问题是 fragment 缺少这一行:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

所以它没有识别出 webview 中高度的变化。

无论如何让我们开始代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    //mWebView.postUrl("https://www.google.com/");
    final View activityRootView = view;
    layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            activityRootView.getWindowVisibleDisplayFrame(r);
            // This variable was created only for Debug purposes and 
            // to see the height change when clicking on a field inside mWebView
            int screenHeight = activityRootView.getRootView().getHeight();
            Log.d("onGlobalLayout", "rect: " + r.toString());
            Log.d("onGlobalLayout", "screenHeight: " + screenHeight);

            //The difference on the heights from bottom to top and on the root height
            int heightDiff = screenHeight - (r.bottom - r.top);
            Log.d("onGlobalLayout", "heightDiff: " + heightDiff);

            //I suggest to put 250 on resources and retrieve from there using getResources().getInteger() to have better order
            float dpx = dpToPx(getActivity(), 250);

            if (previousHeightDiff != heightDiff) {
                if (heightDiff > dpx) {
                    isSoftKeyboardPresent = true;
                } else {
                    isSoftKeyboardPresent = false;
                }
                previousHeightDiff = heightDiff;
            }
        }
    };
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    return view;
}

private static float dpToPx(Context context, float valueInDp) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
}

请记住在您的 AndroidManifest.xml 中设置 Activity 设置为:android:windowSoftInputMode="adjustResize|stateHidden"

fragment 中的变量是:

public boolean isSoftKeyboardPresent = false;
private int previousHeightDiff = -1;// this is used to avoid multiple assignments
private ViewTreeObserver.OnGlobalLayoutListener layoutListener = null;

最后

@Override
public void onPause() {
    super.onPause();
    final View activityRootView = getActivity().findViewById(R.id.page_content);
    activityRootView.getViewTreeObserver().removeOnGlobalLayoutListener(layoutListener);
}

这应该可以解决问题 :) 那么您怎么看?

关于java - 检测键盘何时在 Web View 中处于 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49110467/

相关文章:

Android EditText.setError 无法在无法聚焦的情况下工作

webview - 从 webviews Javascript 调用 C++ 方法

iOS UIWebView 泄露

android - 第二个 onSizeChanged 的​​高度为零

android - onPageFinished() 从未调用过(webview)!

java - 我如何更改代码,以便垃圾收集器将由于程序而删除此实例?

java - API 和软件核心之间交换变量

java - 从较小的字节数组转换为 long (Java)

java - 如果用户具有多个权限,Spring 安全表达式会失败

android - 理解 boundCenterBottom()