当我们专注于输入然后将其显示在软键盘(WebView)上方时,有什么办法吗?

时间:2018-10-04 06:05:59

标签: android android-webview android-softkeyboard

我使用HTML网站加载WebView,但是当用户单击TextBox时,它隐藏在键盘下面。那么对此有何建议?

1 个答案:

答案 0 :(得分:0)

  1. Staus Bar Present

    如果您的活动不是全屏活动,则可以使用android:windowSoftInputMode="adjustResize"或     android:windowSoftInputMode="adjustPan" 在您的AndroidManifest.xml中实现这一目标。

  2. 用于全屏活动

    第一种方法不适用于全屏活动。因此使用必须创建一个实用程序 手动执行此操作的课程

    创建公共类 WindowResize.java

    public  class WindowResize{
       public static void assistActivity(Activity activity) {
        new WindowResize(activity);
       }
    
       private View mChildOfContent;
       private int usableHeightPrevious;
       private FrameLayout.LayoutParams frameLayoutParams;
    
       private WindowResize(final Activity activity) {
        FrameLayout content = activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent(activity);
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
       }
    
      private void possiblyResizeChildOfContent(Activity activity) {
        //Visible height
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            //Total window size. We have to decrement the SoftNavigation button height from total height if there any
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight() - getNavigationButtonHeight(activity);
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard / 4)) {
                // keyboard  visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard  hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
          }
        }
    
      //Usable height in current window.
      private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
      }
    
      //SoftInput navigation buttons height.
      private int getNavigationButtonHeight(Activity activity) {
        // getRealMetrics is only available with API 17 and +
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight)
            return realHeight - usableHeight;
        else
            return 0;
    
      }
    }
    

在包含相应网页视图的主要活动中添加WindowResize.assistActivity(this);