我无法让 ViewActions.closeSoftKeyboard() 在 Espresso 2.2.2 中工作

I can't make ViewActions.closeSoftKeyboard() work in Espresso 2.2.2

我正在尝试测试一个应用程序,我需要隐藏键盘,因为我因此无法单击按钮。所以,我在 build.gradle:

中添加了 Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

并尝试使用 android.support.test.espresso.action.ViewActions 库中的这个:

ViewActions.closeSoftKeyboard();

测试成功运行,但在我的布局中的 EditText 中键入一些文本后失败。键盘还在,正在显示。

P.S。 reading this answer.

后我意识到这是键盘的错

ViewAction 本身不会做任何事情,除非它在 ​​ViewInteraction 中使用。这意味着您需要将它与您之前在 perform() 中的操作链接起来,如下所示: onView()..perform(typeText(..), closeSoftKeyboard()) 或使用 Espresso class 中的内置助手,如下所示: Espresso.closeSoftKeyboard()

你可以实现这个:

fun hideKeyboard() {
    onView(ViewMatchers.isRoot()).perform(ViewActions.closeSoftKeyboard())
  }

之后只用这个:paymentMethodPage.hideKeyboard()