【正确姿势】解决EditText软键盘引起的其他布局移动问题

来源:互联网 发布:机械优化设计 编辑:程序博客网 时间:2024/06/05 16:16

Android软键盘弹出时不把布局顶上去的解决方法

这个问题头疼了我好久,后来一次看其他APP的视频评论才有所启发。

1.第一次启动app都需要登录吧?在登录界面得到软键盘的高度,通过sp储存键盘高度,在登录界面初始化View的时候调用prepareGetSoftKeyBoardHight();

    private void prepareGetSoftKeyBoardHight() {        // 登录界面的根布局        final View root = findViewById(R.id.activity_login);        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                Rect r = new Rect();                // r will be populated with the coordinates of your view that area still visible.                root.getWindowVisibleDisplayFrame(r);                int screenHeight = root.getRootView().getHeight();                int heightDiff = screenHeight - (r.bottom - r.top);                int statusBarHeight = 0;                if (heightDiff > 200){                    // 键盘展开                    try {                        statusBarHeight = UiUtils.getStatusBarHeight();                    } catch (Exception e) {                        e.printStackTrace();                    }                    int realKeyboardHeight = heightDiff - statusBarHeight;                    if (realKeyboardHeight > 200) {                        SharePrefUtil.create().save(SharePrefKey.KEY_BOARD_HIGHT_INT, realKeyboardHeight);                    }                    Logger.d("print", "软键盘高度: " + realKeyboardHeight);                }else {                    // 键盘收起                }                            }        });    }


2.在软键盘弹出引起界面变化,在这个界面监听键盘的打开和关闭,调用下面的函数,大功告成 (监听键盘弹起的网上一大堆,工作忙就不贴出了)

    // 输入框整体的父布局的默认PaddingBottom    private int parentDefPaddingBottom = -9999;    private void changeInputMenuSpace(boolean isDef) {        int keyBoardHight = UiUtils.getKeyBoardHight();        // 输入框整体的父布局        View parent = (View) inputMenu.getParent();        if (parentDefPaddingBottom == -9999) {            parentDefPaddingBottom = parent.getPaddingBottom();        }        if (isDef) {            parent.setPadding(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getPaddingRight(), parentDefPaddingBottom);            return;        }        // 得到输入框整体在屏幕中的位置        int[] viewInScreen = new int[2];        inputMenu.getLocationOnScreen(viewInScreen);                int viewInScreenY = viewInScreen[1];        int viewInScreenBottomY = viewInScreenY + inputMenu.getHeight();                // 输入框整体的底部与屏幕底部的距离        int bootomDis = UiUtils.getScreenHeight() - viewInScreenBottomY;        Logger.d(this, "输入框整体的底部与屏幕底部的距离" + bootomDis);        if (bootomDis >= keyBoardHight) {            return;        }        if (parent.getPaddingBottom() == parentDefPaddingBottom) {            parent.setPadding(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getPaddingRight(), keyBoardHight);        }    }


下面贴出部分代码块,

1.键盘弹起

        inputMenu.getPrimaryMenu().getEditText().setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                if (isInVideo) {                    changeInputMenuSpace(false);                }            }        });


2.键盘展开落下回调里

    @Override    public void OnSoftKeyboardStateChanged(final boolean isKeyBoardShow, int keyboardHeight) {        if (isInVideo) {            hideVideoImCloseIcon(isKeyBoardShow);            if (!isKeyBoardShow) {                changeInputMenuSpace(true);            }                      }    }




阅读全文
0 0
原创粉丝点击