gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:09:09 24 4
gpt4 key购买 nike

使用 uiautomator for Android 我可以在文本字段中设置文本,但无法关闭键盘。对于某些处于横向模式的手机,键盘会占据整个屏幕,必须点击“完成”才能退出该 View 。如果我可以抑制键盘,那么我可以毫无问题地在横向和纵向模式下运行 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();

提前致谢。

最佳答案

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

最佳的是:

if(isKeyboardOpened()){
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressBack();
}

但到目前为止,问题是如何实现 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;
}

关于android - 使用 Android uiautomator 设置文本后抑制键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17223305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com