在Android中打开键盘时调整WebView

时间:2014-07-02 10:43:11

标签: android webview popup android-softkeyboard

我创建了一个应用程序,其中我使用WebView在网站页面上进行导航。

   browser = (WebView) findViewById(R.id.customWebBrowser);

   browser.setWebViewClient(new CustomWebViewClient());
   browser.setWebChromeClient(new CustomWebChromeClient());
   browser.setOnTouchListener(new OnTouchBrowser());

问题是当我点击链接“登录”并打开一个弹出窗口输入登录名/密码。当键盘上升时,它会弹出登录/密码弹出窗口,我看不到我输入的符号,可以调整可见WebView的内容。

我尝试使用AndroidManifest.xml中的 windowSoftInputMode 参数解决此问题,但它没有任何效果。我认为这是因为在服务器端使用弹出窗口。

如何解决此问题,以便在键盘上升时调整屏幕?

2 个答案:

答案 0 :(得分:0)

从IME捕获事件并手动调整WebView高度

答案 1 :(得分:0)

我为我的问题找到了解决方案。它基于yghm的解决方法。

http://code.google.com/p/android/issues/detail?id=5497

public class AndroidBug5497Workaround {

    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    public static void assistActivity (Activity activity) {
        new AndroidBug5497Workaround(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497Workaround(Activity activity) {
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard/4)) {
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }

}

在活动的OnCreate方法中使用此类:

AndroidBug5497Workaround.assistActivity(this);