键盘弹起布局上移的问题

简述

adjustResize关于这个属性其实大家用的很多,就是当我们的键盘弹出时,我们的layout会重新布局。
但是这个属性不是所有情况都适用的。
下面讲讲我遇到的问题 以及解决办法。

我们以常见的评论框为例子。

1、我们先说正常的情况,很简单的布局。点击一个button然后弹出输出框。
Activity的属性

<activity
    android:name=".acitivity.KeyboardActivity"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustResize|stateHidden"
    >
</activity>

Activity_layout布局,弹出键盘后如下图,这个是正常的

Alt text

但是有一种情况,如果我的布局设置足够大的高度时,比如 根布局的高度被定死这个高度

android:layout_height="500dp"

(这里说明一个问题,因为要做一个小红书APP的图文详情的页面。我是用了CoordinatorLayout+viewpager+recyclerView,因为图片最高的大概要有8成屏的高度,所以遇到了adjustresize属性设置后,底部的输入框依然被键盘遮挡住了)

这个时候显示就变成就不正常了,看不到上面输入框了。如下图所示:

Alt text

那是因为布局定死了高度,即使我们设置了adjustResize属性,依然无法达到预期的效果。

解决办法:采用PopupWindow的方式来弹出评论输入框

下面给出代码:
Activity的属性 使用adjustNothing即可

<activity
    android:name=".acitivity.KeyboardActivity"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="使用adjustNothing即可|stateHidden"
    >
</activity>

弹出PopupWindow:

PopupWindow windowComment;
    View viewComment;
    EditText winEditText;
    public void showWindowComment(View view) {
        if (windowComment == null) {
            viewComment = View.inflate(this, R.layout.window_comment_test, null);
            windowComment = new PopupWindow(viewComment, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, true);
            //设置动画效果
            //windowComment.setAnimationStyle(R.style.popwindow_anim);
            //点击其他地方消失
            windowComment.setOutsideTouchable(true);
            windowComment.setFocusable(true);
            windowComment.setBackgroundDrawable(new ColorDrawable(0));
            //这里让设置下PopupWindow的SoftInputMode
            windowComment.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
            windowComment.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            winEditText = (EditText) viewComment.findViewById(R.id.etWin);
            ResizeLayout resizeLayout = (ResizeLayout) viewComment.findViewById(R.id.resizeLayout);
            resizeLayout.setOnKeyBoardListener(new ResizeLayout.onKeyBoardListener() {
                @Override
                public void onKeyBoardShow(int nowHeight) {

                }

                @Override
                public void onKeyBoardHide(int nowHeight) {
                    windowComment.dismiss();

                }
            });
            resizeLayout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    }
                    return false;
                }
            });
        }
        //自动弹起键盘
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                winEditText.setFocusable(true);
                winEditText.setFocusableInTouchMode(true);
                winEditText.requestFocus();
                InputMethodManager inputManager =
                        (InputMethodManager)winEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(winEditText, 0);
            }

        }, 100);

        windowComment.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

ResizeLayout,网上写的监听键盘弹出的布局。


用于监听软键盘的弹起和隐藏

public class ResizeLayout extends RelativeLayout{

    private onKeyBoardListener listener;

    public void setOnKeyBoardListener(onKeyBoardListener listener) {
        this.listener = listener;
    }

    public ResizeLayout(Context context) {
        super(context);
    }

    public ResizeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ResizeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //LogUtil.println("********" + h + "*********" + oldh);
        if (oldh == 0 || h == 0) {
            return;
        }
        if (oldh - h > BaseActivity.dip2px(getContext(), 100)) {
            if (listener != null) {
                listener.onKeyBoardShow(h);
                return;
            }
        }
        if (listener != null) {
            listener.onKeyBoardHide(h);
        }
    }

    public interface onKeyBoardListener{
        void onKeyBoardShow(int nowHeight);
        void onKeyBoardHide(int nowHeight);
    }
}

PS:自动弹起键盘参考网上代码。

总结:其实这个问题不复杂,开始的时候没想到利用弹出窗来实现。在这里做个记录。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值