6

我有一个 android 应用程序,它使用 uiautomator 单击列表视图中的选项。ListView 如下所示:

在此处输入图像描述

我正在尝试单击完整基准列表项,但我的代码无法识别该列表项。这就是我所拥有的:

UiScrollable listView = new UiScrollable(new UiSelector().scrollable(
        true).className("android.widget.ListView"));

UiObject item1 = listView.getChildByText(new UiSelector()
.className(android.widget.TextView.class.getName()),
        "Full Benchmark");

item1.click();

我将不胜感激任何帮助!

4

3 回答 3

9

这是我用来查找的内容,然后单击列表视图中的项目:

//Find and click a ListView item
public void clickListViewItem(String name) throws UiObjectNotFoundException {
    UiScrollable listView = new UiScrollable(new UiSelector());
    listView.setMaxSearchSwipes(100);
    listView.scrollTextIntoView(name);
    listView.waitForExists(5000);
    UiObject listViewItem = listView.getChildByText(new UiSelector()
            .className(android.widget.TextView.class.getName()), ""+name+"");
    listViewItem.click();
    System.out.println("\""+name+"\" ListView item was clicked.");
}

所以在你的情况下

clickListViewItem("Full Benchmark")

或者:

UiScrollable listView = new UiScrollable(new UiSelector());
listView.setMaxSearchSwipes(100);
listView.scrollTextIntoView(name);
listView.waitForExists(5000);
UiObject listViewItem = listView.getChildByText(new UiSelector()
        .className(android.widget.TextView.class.getName()), "Full Benchmark");
listViewItem.click();
于 2013-06-14T20:04:32.760 回答
2

上述事情有两种方法。

方法一:

  1. 分析 UI,然后获取列表视图的实例,说包含文本的列表视图是第三个列表视图
  2. 创建一个 UIcollection 对象,如UiCollection dummyList = new UiCollection(new UiSelector().className("android.widget.ListView").index(3));
  3. 为 Benchmark 创建一个对象UiObject dummyBenchmark = dummyList.getChildByText("Full Benchmark");

方法二:

new UiScrollable(
    new UiSelector().scrollable(true)
).scrollIntoView(
    new UiSelector().text("Full BenchMark")
);
new UiObject(
    new UiSelector().text("Full BenchMark")
).click();
于 2013-09-07T11:28:04.867 回答
0

嵌套的 ViewGroup 内有两个 TextView,很可能是 LinearLayout。选择标准中似乎缺少这一点。

于 2013-06-08T00:15:08.620 回答