通过对编辑窗口的根布局高度变化来判断软键盘的显示和隐藏状态变化
需要自己重写根布局,重写onSizeChanged方法
public class ResizeRelativeLayout extends RelativeLayout{ private OnResizeRelativeListener listener; public ResizeRelativeLayout(Context context) { super(context); } public ResizeRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } public ResizeRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if(listener != null){ listener.onResizeRelative(w, h, oldw, oldh); } } public interface OnResizeRelativeListener{ void onResizeRelative(int w, int h, int oldw, int oldh); } public void setOnResizeRelativeListener(OnResizeRelativeListener listener){ this.listener = listener; } }
然后在代码中添加监听器,做相应的处理
commentLayout.setOnResizeRelativeListener(new ResizeRelativeLayout.OnResizeRelativeListener() { @Override public void onResizeRelative(int w, int h, int oldw, int oldh) { LogW.out("w=" + w + ",h=" + h + ",oldw=" + oldw + ",oldh=" + oldh); if(oldh != 0 && h-oldh > 0){ hideCommentLayout(); } } });