如何使用uiautomator获取视图的父级?

Gab*_*umb 15 android android-uiautomator

我正在尝试识别ui元素的父视图,以便我可以自由地浏览UI.

例如,在"设置"应用中,我可以找到带有"蓝牙"文本的视图:

UiObject btView = new UiObject(new UiSelector().text("Bluetooth"));
Run Code Online (Sandbox Code Playgroud)

现在,我遇到困难的部分是这样的:我想要导航两个级别并开始新的搜索开启/关闭按钮,启用和禁用蓝牙.

注意:如果我使用下面的代码,我可以获得按钮.

UiObject btButtonView = new UiObject(new UiSelector().className("android.widget.Switch").instance(1));
Run Code Online (Sandbox Code Playgroud)

这将搜索切换按钮并返回第二次遭遇.我希望搜索更精确,并在包含"蓝牙"文本的线性布局中查找按钮.

更新:这是设置应用程序的布局(我需要的蓝牙部分):

LinearLayout
    LinearLayout
        ImageView
    RelativeLayout
        TextView (with text = "Bluetooth")
    Switch ()
Run Code Online (Sandbox Code Playgroud)

And*_*ers 15

您需要首先使用文本找到UiObject两个级别.这可以使用getChildByText()UiCollection或UiScrollable中的方法完成.然后你可以很容易地找到开关.对于"设置",此代码适用于我的设备:

UiScrollable settingsList = new UiScrollable(new UiSelector().scrollable(true));
UiObject btItem = settingsList.getChildByText(new UiSelector().className(LinearLayout.class.getName()),"Bluetooth", true);

UiObject btSwitch = btItem.getChild(new UiSelector().className(android.widget.Switch.class.getName()));
btSwitch.click();
Run Code Online (Sandbox Code Playgroud)