Android 如何在不同布局上选择具有相同id的ToggleButton

Android 如何在不同布局上选择具有相同id的ToggleButton,android,android-layout,selenium,mobile,ios-ui-automation,Android,Android Layout,Selenium,Mobile,Ios Ui Automation,我有以下层次结构: 您知道如何与第二个切换按钮交互吗?第二个线性布局下的按钮(8)? 请注意,两个切换按钮具有相同的id/类别。唯一的区别是按钮上的文本 谢谢你的帮助 您可以为每个ToggleButton分配不同的标记,以便使用 单独访问按钮。最好先获取封闭元素: LinearLayout enclosingLayout = (LinearLayout) view.findViewById(R.id.enclosungElement1); 然后使用findViewById查找子视图: Tog

我有以下层次结构:

您知道如何与第二个切换按钮交互吗?第二个线性布局下的按钮(8)? 请注意,两个切换按钮具有相同的id/类别。唯一的区别是按钮上的文本


谢谢你的帮助

您可以为每个ToggleButton分配不同的标记,以便使用


单独访问按钮。

最好先获取封闭元素:

LinearLayout enclosingLayout = (LinearLayout) view.findViewById(R.id.enclosungElement1);
然后使用findViewById查找子视图:

ToggleButton button = (ToggleButton) enclosingLayout.findViewById(R.id.yourToggleButton);

如果我没弄错的话

  • 您可以对视图使用SetTag,稍后使用fiViewByTag(视图的标记)进行查找

  • 只需找到父线性布局,如findviewbyd(LinearLayout(8))。 并将chield作为parent.findviewbyd(切换按钮)