Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在webview中检测键盘何时处于活动状态_Java_Android_Webview_Keyboard_Android Webview - Fatal编程技术网

Java 在webview中检测键盘何时处于活动状态

Java 在webview中检测键盘何时处于活动状态,java,android,webview,keyboard,android-webview,Java,Android,Webview,Keyboard,Android Webview,我还没有找到任何方法来解决这个问题 我的应用程序中有一个网络视图。我希望能够检测键盘何时处于活动状态,何时处于非活动状态。当这些变化发生在网络视图中时,它似乎无法检测到 我想对这些不同的状态执行操作。在iOS上,当键盘处于活动状态时,通过观察者进行监听非常简单。Ref UIKeyboard将显示/隐藏 android中是否有任何功能与这些观察者在android中的功能相同 希望这个问题解释得足够好 所以我花了一周左右的时间来解决这个问题,发现这里有很多资料是我的解决方案,我真的希望它对你有用 所

我还没有找到任何方法来解决这个问题

我的应用程序中有一个网络视图。我希望能够检测键盘何时处于活动状态,何时处于非活动状态。当这些变化发生在网络视图中时,它似乎无法检测到

我想对这些不同的状态执行操作。在iOS上,当键盘处于活动状态时,通过观察者进行监听非常简单。Ref UIKeyboard将显示/隐藏

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


希望这个问题解释得足够好

所以我花了一周左右的时间来解决这个问题,发现这里有很多资料是我的解决方案,我真的希望它对你有用

所以在我的例子中,我有一个活动,我调用了一个包含Webview的片段,所以它比我想象的要复杂得多

基本上,问题在于片段缺少这一行:

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上设置活动集:
android:windowSoftInputMode=“adjustResize | stateHidden”

片段内的变量为:

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

这应该可以解决问题:)那么你觉得呢?

可能重复的“别这么想”。这些答案似乎包括本机内置的“EditText”,我有一个带有文本字段的webview。我需要知道这些字段何时处于活动状态。这在ios上很简单,但在安卓上似乎有更多。为什么不呢?我假设您的web视图有文本字段,默认情况下会打开键盘。您可以将侦听器附加到webview。你可以试试,我觉得应该行得通。