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

沉浸式状态栏下,点击自定义输入框布局,软键盘弹出后,只定义输入框被覆盖,在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;

  }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容