使用Espresso单击ListView中的特定复选框 [英] Click on a specific checkbox in a ListView using Espresso
问题描述
我有一个ListView
,其中每一行都有一个复选框:
I have a ListView
where each row has a checkbox:
现在,我想单击第四行的复选框.我有每一行的数据模型,因此我可以轻松地使用onData()
选择具有给定数据的行.但是,如何单击该行中的复选框?
Now I want to click on the checkbox in the 4th row. I have the data model for each row, so I can easily use onData()
to select a row with the given data. But how do I click on the checkbox in that row?
推荐答案
如果您的行布局允许单击该行以设置CheckBox,则可以使用它来单击ListView行:
If your row layout allows clicking on the row to set the CheckBox, you can use this to click the ListView row:
onData(anything()).atPosition(4).perform(click());
否则,您可以在不知道其ID的情况下直接单击CheckBox:
Otherwise you can click directly on the CheckBox without knowing its ID:
onData(anything())
.atPosition(4)
.onChildView(withClassName(Matchers.containsString("CheckBox")))
.perform(click());
然后您可以断言CheckBox已选中:
You can then assert that the CheckBox is checked:
onData(anything())
.atPosition(4)
.onChildView(withClassName(Matchers.containsString("CheckBox")))
.check(matches(isChecked()));
更多信息: https://github.com/shohrabuddin/Espresso
注意:要快速添加这些方法的导入,请将闪烁的光标放在未解决的方法上,然后执行Android Studio➔ Help ➔ Find Action ➔搜索"show context action"
或"show intention action"
➔单击结果选项➔将出现一个弹出窗口➔单击"Import static method ..."
.您还可以将键盘快捷方式分配给显示上下文操作". 此处了解更多信息.另一种方法是在设置"中启用"Add unambiguous imports on the fly"
.
Note: To quickly add the imports for these methods, put the blinking cursor on the unresolved method, then do Android Studio ➔ Help ➔ Find Action ➔ search for "show context action"
or "show intention action"
➔ click on the result option ➔ A popup window will appear ➔ click on "Import static method ..."
. You can also assign a keyboard shortcut to "Show Context Actions". More info here. Another way is to enable "Add unambiguous imports on the fly"
in the Settings.
这篇关于使用Espresso单击ListView中的特定复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!