软键盘挡住

作者: zhongjh | 来源:发表于2020-07-19 01:22 被阅读0次

    当软键盘出现遮挡住布局后,如果修改成SOFT_INPUT_ADJUST_PANSOFT_INPUT_ADJUST_RESIZE依然解决不了想要的需求,那么下面这个就是最合适解决软键盘挡住的方案了。
    解决方案大概要述:把布局修改成滑动式布局,当出现软键盘后,将最外面的布局高度缩至软键盘上面,并且整体布局依然可以保持滑动。

    对本身代码侵略性也少,一句代码放在Activity即可。

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ```
            Softkeyboard.assistActivity(rlContent);
            ```
        }
    

    布局

    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/rlContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <include layout="@layout/newui_title_bar" />
    
            <ScrollView
                android:id="@+id/scrollView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/title_layout"
                android:animateLayoutChanges="true"
                android:orientation="vertical">
    
            </ScrollView>
    
        </RelativeLayout>
    

    Softkeyboard

    public class Softkeyboard {
    
        // For more information, see https://issuetracker.google.com/issues/36911528
        // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
    
        public static void assistActivity(Activity activity) {
            new Softkeyboard(activity);
        }
    
        public static void assistActivity(RelativeLayout rlContent) {
            new Softkeyboard(rlContent);
        }
    
        private View mChildOfContent;
        private int usableHeightPrevious;
        private FrameLayout.LayoutParams frameLayoutParams;
    
        private Softkeyboard(Activity activity) {
            FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
            mChildOfContent = content.getChildAt(0);
            mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(() -> possiblyResizeChildOfContent());
            frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
        }
    
        private Softkeyboard(RelativeLayout rlContent) {
            mChildOfContent = rlContent;
            mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(() -> 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 / 5)) {
                    // 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);
        }
    
    }
    

    在这里对Softkeyboard的一段代码usableHeightSansKeyboard / 5说一下,如果包含导航栏也加入滑动布局的话,就 usableHeightSansKeyboard / 4

    相关文章

      网友评论

        本文标题:软键盘挡住

        本文链接:https://www.haomeiwen.com/subject/cctchktx.html