在利用Robotium做功能自动化测试的时候,常常由于一些EditText等的虚拟键盘弹出问题,导致额外的工作量来处理虚拟键盘的问题。
现在指定一种处理虚拟键盘的方法:
InputMethodManager:
Central system API to the overall input method framework (IMF) architecture, which arbitrates interaction between applications and the current input method. You can retrieve an instance of this interface with .
InputMethodManager是用来控制输入法的:
Java代码 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
用:
Java代码 if (imm.isActive())
来检查虚拟键盘是不是在开启的状态
然后可以用toggle方法来关闭它
Java代码 imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
当然如果当前虚拟键盘是关闭的, toggle就会开启键盘.
InputMethodManager也有独立开启和关闭的方法:
Java代码 imm.showSoftInput(view, flags);
imm.showSoftInputFromInputMethod(token, flags);
imm.hideSoftInputFromInputMethod(token, flags);
在robotium种如此处理:
InputMethodManager imm = (InputMethodManager) solo.getCurrentActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
assertTrue(imm.isActive());