UiAutomator访问信息有限的孩子

时间:2013-11-13 21:29:09

标签: android uiautomator

我有一个动态构建的视图列表,因此它们都具有完全相同的资源ID,并且没有描述。我可以区分它们之间区别的唯一方法是在同一个父级中的文本。我认为唯一的办法就是这样做:

Child >> parent >> child

enter image description here

如您所见,我可以让孩子MILESCALORIES。我需要从大数字中获取文本。

1 个答案:

答案 0 :(得分:1)

可以获得大数字视图中的文本。我们假设布局是:

1. RelativeLayout
    0. Text View -> MILES
    1. Text View -> 0.50
2. RelativeLayout
    0. Text View -> CALORIES
    1. Text View -> 100

UiCollection resultsPage = new UiCollection(
            new UiSelector().className(android.widget.ListView.class.getName()));
UiObject resultLayout = resultsPage
            .getChildByText(new UiSelector()
                    .className(android.widget.RelativeLayout.class
                            .getName()), text); //where text can be MILES or CALORIES
UiObject score = resultLayout.getChild(new UiSelector()
            .resourceId(the resource id for the large text));
return (String) score.getText();

这个想法是隔离两个相对布局并搜索其中的文本,而不是整个GUI。 您可以使用uiautomatorviewer查看GUI布局。也许你可以用详细的布局更新问题。

我可以使用它来提供更准确的答案。