使用 Android uiautomator 设置文本后抑制键盘

Dan*_*iel 5 android android-uiautomator

使用 uiautomator for Android 我可以在文本字段中设置文本,但无法关闭键盘。对于某些手机,当处于横向模式时,键盘会占据整个屏幕,必须点击“完成”才能退出该视图。如果我可以抑制键盘,那么我可以在横向和纵向运行 uiautomator 没有问题。

new UiObject(new UiSelector().text("Enter Text")).click();
new UiObject(new UiSelector().className("android.widget.EditText").instance(0)).setText("sample text");

// This is where I need to suppress the keyboard to view the app instead of just the keyboard itself.

new UiObject(new UiSelector().text("Submit")).click();
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Inê*_*nês 6

这是一个相当古老的问题,但使用 UiAutomator 2.0 可以正确、完整地回答问题,因此就在这里。

最佳的将是:

if(isKeyboardOpened()){
    UiDevice.pressBack();
}
Run Code Online (Sandbox Code Playgroud)

但到目前为止,问题是如何实现 isKeyboardOpened()。

由于 UiAutomator 2.0 基于仪器,因此我们可以访问 UiAutomation,我们可以验证屏幕上是否存在任何输入窗口:

boolean isKeyboardOpened(){
    for(AccessibilityWindowInfo window: InstrumentationRegistry.getInstrumentation().getUiAutomation().getWindows()){
        if(window.getType()==AccessibilityWindowInfo.TYPE_INPUT_METHOD){
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)


And*_*ers 2

通常单击返回键将关闭键盘。

getUiDevice().pressBack();
Run Code Online (Sandbox Code Playgroud)

  • 有什么智能化的解决方案吗?IE。涉及检查键盘是否显示、键盘是否阻碍按钮点击等。 (3认同)