美文网首页
解决布局与输入框隐藏的尴尬问题(二)完

解决布局与输入框隐藏的尴尬问题(二)完

作者: 宾哥来啦 | 来源:发表于2018-05-11 15:12 被阅读0次

    主要解决的问题:

    页面布局中使用了scrollview,也有输入框,当比较靠底的输入框获取焦点后,输入框会被键盘遮住,无法滑动看到被键盘遮住的输入框,使用以下代码,可解决此问题

    注意:不需要在manifest文件中设置activity的windowSoftInputMode属性,只需使用下面这一句话就可搞定

    调用部分:

    SoftKeyInputHidWidget.assistActivity(this);//修复键盘遮挡问题,必须放在setconentView()后面

    核心代码(SoftKeyInputHidWidget):

    public class SoftKeyInputHidWidget {

    /**

    * 用于解决因为沉浸式状态栏,但不想采用fitSystemWidnow属性

    * 或不想设置键盘属adjustResize/adjustPan属性,想实现键盘不遮挡输入框

    * 相关说明:https://blog.csdn.net/smileiam/article/details/69055963

    * Created by SmileXie on 2017/3/27.

    */

    private ViewmChildOfContent;

    private int usableHeightPrevious;

    private FrameLayout.LayoutParamsframeLayoutParams;

    private int contentHeight;

    private boolean isfirst =true;

    private int statusBarHeight;

    public static void assistActivity(Activity activity) {

    new SoftKeyInputHidWidget(activity);

    }

    private SoftKeyInputHidWidget(Activity activity) {

    statusBarHeight =getStatusBarHeight(activity);

    FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);

    mChildOfContent = content.getChildAt(0);

    //界面出现变动都会调用这个监听事件

            mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    public void onGlobalLayout() {

    if (isfirst) {

    contentHeight =mChildOfContent.getHeight();//兼容华为等机型

                        isfirst =false;

    }

    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

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference +statusBarHeight;

    }else {

    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;

    }

    }else {

    frameLayoutParams.height =contentHeight;

    }

    mChildOfContent.requestLayout();

    usableHeightPrevious = usableHeightNow;

    }

    }

    /**

    * 获取改变之后界面的可用高度(可以为开发者显示内容的高度)

    *

        * @return

        */

        private int computeUsableHeight() {

    Rect r =new Rect();

    mChildOfContent.getWindowVisibleDisplayFrame(r);//获取到的rect就是界面除去标题栏、除去软键盘挡住部分,所剩下的域

            return (r.bottom - r.top);

    }

    public static int getStatusBarHeight(Activity activity) {

    //获取状态栏的高度

            int resourceId = activity.getResources().getIdentifier("status_bar_height","dimen","android");

    return activity.getResources().getDimensionPixelSize(resourceId);

    }

    }

    补充说明:

    activity的xml布局格式:

    具体原理解释请看这位博主的详细讲解:

    https://blog.csdn.net/smileiam/article/details/69055963###;

    相关文章

      网友评论

          本文标题:解决布局与输入框隐藏的尴尬问题(二)完

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