一、布局监听输入法状态

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

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();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,025评论 3 119
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,932评论 25 709
  • 不知不觉,岁寒输入法的更新历史已经可以列出这么一长串来了。从中可以看出,岁寒的发展过程也是一个不断试错的过程,其中...
    临岁之寒阅读 34,227评论 1 6
  • 董文晓(网名白饭)*观鸟超过10年,于2010年成立华夏荒野旅行(China Wild Tour),正式当起全职鸟...
    Jennifer_Leung阅读 4,879评论 1 0
  • 时常在深夜感叹以前的点点滴滴,模糊到点着蜡烛走夜路去上学,记不清了,不只是走过的路,还有那淡去的同伴,清晰到和...
    小脚丫丫阅读 188评论 0 0