前项目需求需要监听在输入法弹起和隐藏的时候,更改界面的相关显示。一开始控制界面是否显示是调用自己写的工具类。
也就是自己知道什么时候会强制调用KeyBord的close和open方法,因此一切还很完美。
结果,坑爹的搜狗输入法右上角竟然有一个隐藏输入法的按钮,我却监听不到这个按钮事件。
一开始我的思路是盯着如何捕获键盘按钮事件,整的头大。还是众人拾柴火焰高,同事找到了其他方法,即监听当前界面绘制布局的改变。这里我简单包装了一下。下面就是我自定义的输入法状态监听类,
也就是自己知道什么时候会强制调用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>