[Android]监听输入框移动布局避免键盘遮盖提交按钮

来源:互联网 发布:ubuntu vmdk 下载 编辑:程序博客网 时间:2024/05/18 14:27

当提交按钮在输入框下方的时候,通过设置EditText的属性并不能达到避免键盘遮盖提交按钮的目的,此时需要通过监听界面,当有键盘弹出时调成界面的位置,代码如下:

1.在onResume方法中为DecorView设置布局变化监听器:

@Overrideprotected void onResume() {    super.onResume();    getWindow().getDecorView().addOnLayoutChangeListener(onLayoutChangeListener);}

2.监听器的实现:

/** * 调整布局 * @param isVisible */private void adjustLayout(boolean isVisible){    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)img_title.getLayoutParams();    if(isVisible){        if(params.topMargin!=108){            params.setMargins(0,108,0,0);            editText.setLayoutParams(params);        }    }else{        if(params.topMargin!=10){            params.setMargins(0,10,0,0);            editText.setLayoutParams(params);        }    }}/** * 布局监听器 */private View.OnLayoutChangeListener onLayoutChangeListener = new View.OnLayoutChangeListener() {    @Override    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {        //获取View可见区域的bottom        Rect rect = new Rect();        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);        if(rect.bottom>1000){            adjustLayout(true);        }else{            adjustLayout(false);        }    }};