android自定义输入法判断是否在输入状态的问题

来源:互联网 发布:ubuntu 文件夹root权限 编辑:程序博客网 时间:2024/06/05 02:33
工作中遇到一个需要判断输入法是否是在输入的状态的需求,输入法不需要显示在界面。
既然不需要输入法界面,那么让onCreateInputView时返回空。
检查inputmethodservice的API发现InputMethodService中有 onStartInput,showWindow和hideWindow,onFinishInput,onWindowShown,onWindowHidden,isInputViewShown等方法
根据经验判断onWindowShown和onWindowHidden应该是输入法触发输入和隐藏的回调.
实际测试结果
1.onWindowShown没有被调用,onWindowHidden有被调用。
2.showWindow没有被调用,而hideWindow有被调用。
3.在普通edittext中触发输入法onStartInput有被调用,而在网页中触发焦点时onStartInput没调用一次,把焦点移到网页中的空白部分,触发hideWind,再触发网页输入框焦点没有被调用onStartInput,也没调用showWindow
4.isInputViewShown无法是否在输入状态都返回false.
5.onFinishInput方法在输入法隐藏时有被调用。
这样的API太不可靠了。
难道就没有办法了吗?继续从网上下载开源的OpenWnn输入法,发现它的showWindow和hideWindow方法回调非常准确。
把给输入法加上了inputview。但是又遇到onCreateInputView没有被调用的问题,检查android api发现这样一段话。
http://www.apihome.cn/api/android/InputMethodService.html

onEvaluateInputViewShown

public boolean onEvaluateInputViewShown()
Override this to control when the soft input area should be shown to the user. The default implementation only shows the input view when there is no hard keyboard or the keyboard is hidden. If you change what this returns, you will need to call updateInputViewShown() yourself whenever the returned value may have changed to have it re-evalauted and applied.

原来默认的实现是只有在没有物理键盘或者键盘隐藏时才返回显示输入法视图。因此解决方法是关闭物理键盘或者重写这个方法,永久返回true.
解决了上面的问题后onCreateInputView有被调用。同时showWindow和hideWindow方法也有被正确调用。在showWindow和hideWindow方法中可以准确的判断输入法是否有在输入的状态。
0 0