沉浸式状态栏下,自定义输入框布局无法被软键盘顶起的问题

2017-07-27  本文已影响159人  牛奶糖_王小二

沉浸式状态栏下,点击自定义输入框布局,软键盘弹出后,只定义输入框被覆盖,在manifest文件中设置windowSoftInputMode也没有解决,下面是解决方案:

写一个类,然后再需要弹出软键盘和布局的activity里面setContentView后写上SolveEditText.assistActivity(this)就可以了。

public classSolveEditText {

                public static voidassistActivity(Activity activity) {

                               newSolveEditText(activity);

                 }

   private View mChildOfContent;

   private int usableHeightPrevious;

   private FrameLayout.LayoutParams frameLayoutParams;

   private SolveEditText(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);

returnr.bottom;

  }

}

上一篇下一篇

猜你喜欢

热点阅读