【发布时间】:2019-07-09 10:11:07
【问题描述】:
这个问题是针对Android开发中使用的Kotlin的扩展功能。
所以 Kotlin 为我们提供了将某些扩展行为添加到类中以扩展基础类行为的能力。
示例:(取自我当前的 Android 项目,用于在 Espresso 测试中的 viewAssertion)
fun Int.viewInteraction(): ViewInteraction {
return onView(CoreMatchers.allOf(ViewMatchers.withId(this), ViewMatchers.isDisplayed()))
}
在我的用例中,我可以像这样使用它:
R.id.password_text.viewInteraction().perform(typeText(PASSWORD_PLAIN_TEXT), pressDone())
一切都好,除了这个扩展函数为所有Int对象提供了扩展行为,而不仅仅是Android中的View ID,这一点都不好。
问题是是否有任何方法可以为这个Int 提供上下文,就像在Android 中我们有@IdRes in Android support annotation 用于上述给定的情况?
【问题讨论】:
标签: android kotlin extension-function