Android 监听输入法隐藏与显示状态切换

前项目需求需要监听在输入法弹起和隐藏的时候,更改界面的相关显示。一开始控制界面是否显示是调用自己写的工具类。
这里写图片描述
也就是自己知道什么时候会强制调用KeyBord的close和open方法,因此一切还很完美。
结果,坑爹的搜狗输入法右上角竟然有一个隐藏输入法的按钮,我却监听不到这个按钮事件。
一开始我的思路是盯着如何捕获键盘按钮事件,整的头大。还是众人拾柴火焰高,同事找到了其他方法,即监听当前界面绘制布局的改变。这里我简单包装了一下。下面就是我自定义的输入法状态监听类,
<span style="font-size:18px;color:#3333FF;"><code class="hljs java has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">onInputLayoutChangeListener</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">ViewTreeObserver</span>.<span class="hljs-title">OnGlobalLayoutListener</span> {</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> UnifyLog lg = UnifyLogFactory.getLogger(onInputLayoutChangeListener.class.getSimpleName());

    <span class="hljs-keyword">private</span> View rootView;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> curRectHeight = -<span class="hljs-number">1</span>;

    <span class="hljs-keyword">public</span> <span class="hljs-title">onInputLayoutChangeListener</span>(View rootView) {
        <span class="hljs-keyword">this</span>.rootView = rootView;
    }

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onGlobalLayout</span>() {
        Rect rect = <span class="hljs-keyword">new</span> Rect();
        rootView.getWindowVisibleDisplayFrame(rect);
        <span class="hljs-keyword">int</span> displayHight = rect.bottom - rect.top;
        <span class="hljs-keyword">int</span> height = rootView.getHeight();
        lg.e(<span class="hljs-string">"RectHeight:"</span> + displayHight + <span class="hljs-string">",DecorViewHeigt:"</span> + height);
<span class="hljs-comment">//        if (curRectHeight != displayHight) {</span>
<span class="hljs-comment">//            curRectHeight = displayHight;</span>
<span class="hljs-comment">//            onLayoutChange(displayHight, height);</span>
<span class="hljs-comment">//        }</span>
        onLayoutChange(displayHight, height);
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onLayoutChange</span>(<span class="hljs-keyword">int</span> intputTop, <span class="hljs-keyword">int</span> windowHeight);
}</code></span>

调用方式:

<span style="font-size:18px;color:#3333FF;"><code class="hljs java has-numbering">getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(<span class="hljs-keyword">new</span> onInputLayoutChangeListener(getWindow().getDecorView()) {
            <span class="hljs-annotation">@Override</span>
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onLayoutChange</span>(<span class="hljs-keyword">int</span> intputTop, <span class="hljs-keyword">int</span> windowHeight) {
                edtInputContent.setY(intputTop - edtInputContent.getMeasuredHeight());
            }
        });</code></span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值