Android 开发 软键盘覆盖而不上推布局 控件总显示在软件盘上边

场景底部edittext点击之后会上推整个布局
解决方案

/**
     * 打开软键盘的
     * @param editText
     */
    private void showInputManager(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();

        /** 目前测试来看,还是挺准的
         * 原理:OnGlobalLayoutListener 每次布局变化时都会调用
         * 界面view 显示消失都会调用,软键盘显示与消失时都调用
         * */
        mRootView.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutChangeListener);
        InputMethodManager inputManager =
                (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

    }

mRootView指整个布局,

 ViewTreeObserver.OnGlobalLayoutListener mLayoutChangeListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            int height = wm.getDefaultDisplay().getHeight();
            //判断窗口可见区域大小
            Rect r = new Rect();
            // getWindowVisibleDisplayFrame()会返回窗口的可见区域高度
            getWindow().getDecorView().getWindowVisibleDisplayFrame(r);

            //如果屏幕高度和Window可见区域高度差值大于整个屏幕高度的1/3,则表示软键盘显示中,否则软键盘为隐藏状态。
            int heightDifference = height - (r.bottom - r.top);
            boolean isKeyboardShowing = heightDifference > height / 3;
            if (isKeyboardShowing) {
//                D.i("slack","show..."+ r.bottom + " - " + r.top + " = " + (r.bottom - r.top) +","+ heightDifference);
                // bottomView 需要跟随软键盘移动的布局
                // setDuration(0) 默认300, 设置 0 ,表示动画执行时间为0,没有过程,只有动画结果了
                buttomLlDetails.setVisibility(View.GONE);
                etEnter.setVisibility(View.VISIBLE);
                etEnter.setFocusable(true);
                etEnter.setFocusableInTouchMode(true);
                etEnter.requestFocus();
                //上边这些不重要 主要是下边的 buttomSvDetails是包裹我的edittext的父控件一个scrollview让edittext移动软键盘的距离,是这个控件可以一直保持在软键盘的上边
                buttomSvDetails.animate().translationY(-heightDifference).setDuration(0).start();
            } else {
//                D.i("slack","hide...");
                buttomSvDetails.animate().translationY(0).setDuration(0).start();

                if(mToolbar.getVisibility()==View.VISIBLE){
                    buttomLlDetails.setVisibility(View.VISIBLE);
                }
                etEnter.setVisibility(View.GONE);

            }
        }
    };
<ScrollView
        android:id="@+id/sv_comment"
        android:padding="@dimen/y9"
        android:background="#348EB4D9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:padding="@dimen/y5"
            android:background="@drawable/shape_bg_et_details"
            android:id="@+id/et_comment"
            android:imeOptions="actionSend"
            android:singleLine="true"
            android:inputType="text"
            android:focusableInTouchMode="false"
            android:hint="写评论"
            android:editable="false"
            android:clickable="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

布局文件 要跟随软键盘移动的控件放到scrollview的里边

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值