Android

一、布局监听输入法状态

2018-11-14  本文已影响0人  千年白骨精

需求:监听输入法的显示与隐藏

public class InputMethodStateRelativeLayout extends RelativeLayout {

    private int initBottom = -1;
    private int initLeft = -1;

    private OnInputMethodChangedListener onInputMethodChangedListener;

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

    public InputMethodStateRelativeLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (initBottom == -1 || initLeft == -1) {
            initBottom = b;
            initLeft = l;
            return;
        }
        if (changed) {
            int height = b - initBottom; //高度变化值(弹出输入法,布局变小,则为负值)
            Logger.d("height:" + height);
            if (null != onInputMethodChangedListener) {
                if (height < 0) {
                    onInputMethodChangedListener.onShowInputMethodListener();
                } else {
                    onInputMethodChangedListener.onHiddenInputMethodListener();
                }
            }
        }
    }

    public void setOnInputMethodChangedListener(OnInputMethodChangedListener
                                                        onInputMethodChangedListener) {
        this.onInputMethodChangedListener = onInputMethodChangedListener;
    }

    public interface OnInputMethodChangedListener {
        void onShowInputMethodListener();
        void onHiddenInputMethodListener();
    }
}
上一篇下一篇

猜你喜欢

热点阅读