我希望在活动调用onCreate()和onResume()时测试键盘可见性。
如何测试是否使用浓咖啡显示键盘?
答案 0 :(得分:2)
fun isKeyboardShown(): Boolean {
val inputMethodManager = InstrumentationRegistry.getInstrumentation().targetContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
return inputMethodManager.isAcceptingText
}
答案 1 :(得分:0)
另一个技巧可能是检查键盘显示时你知道将要覆盖的视图的可见性。不要忘记考虑动画......
使用espresso和hamcrest进行仪器测试,用于NOT匹配器:
//make sure keyboard is visible by clicking on an edit text component
ViewInteraction v = onView(withId(R.id.editText));
ViewInteraction v2 = onView(withId(R.id.componentVisibleBeforeKeyboardIsShown));
v2.check(matches(isDisplayed()));
v.perform(click());
//add a small delay because of the showing keyboard animation
SystemClock.sleep(500);
v2.check(matches(not(isDisplayed())));
hideKeyboardMethod();
//add a small delay because of the hiding keyboard animation
SystemClock.sleep(500);
v2.check(matches(isDisplayed()));
答案 2 :(得分:0)
我知道,这个问题已经足够老了,但是却没有任何可接受的答案。 在我们的UI测试中,我们使用此方法,该方法使用一些shell命令:
/**
* This method works like a charm
*
* SAMPLE CMD OUTPUT:
* mShowRequested=true mShowExplicitlyRequested=true mShowForced=false mInputShown=true
*/
fun isKeyboardOpenedShellCheck(): Boolean {
val checkKeyboardCmd = "dumpsys input_method | grep mInputShown"
try {
return UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
.executeShellCommand(checkKeyboardCmd).contains("mInputShown=true")
} catch (e: IOException) {
throw RuntimeException("Keyboard check failed", e)
}
}
希望,对某人有用
答案 3 :(得分:0)
这对我有用。
private boolean isSoftKeyboardShown() {
final InputMethodManager imm = (InputMethodManager) getActivityInstance()
.getSystemService(Context.INPUT_METHOD_SERVICE);
return imm.isAcceptingText();
}
@igork 答案的 Java 版本。
答案 4 :(得分:0)
这个方法对我有用
val isKeyboardOpened: Boolean
get() {
for (window in InstrumentationRegistry.getInstrumentation().uiAutomation.windows) {
if (window.type == AccessibilityWindowInfo.TYPE_INPUT_METHOD) {
return true
}
}
return false
}
答案 5 :(得分:-3)
这是检查键盘是否可见的一种技巧,它不是一个完美的解决方案,但对我来说已经足够了:
简单的代码示例:
onView(allOf(withId(R.id.myFragment),isDisplayed()));
onView(withId(R.id.myFragment)).perform(pressBack());
onView(allOf(withId(R.id.myFragment),isDisplayed()));
如果键盘可见,则意味着第二次按下后退按钮时,视图容器仍然存在;)
希望这有帮助!