【发布时间】:2022-04-04 10:19:26
【问题描述】:
如何从具有相同 ID 的不同文本视图中单击单个文本视图。这些是线性布局的子级。它们的文本像 A、B、C、D 一样动态地提供给 id 为 txt_option_index 的 textview。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:clickable="true"
android:id="@+id/option_rl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/border">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/topMarginU2"
android:layout_centerVertical="true"
android:id="@+id/option_sign_bg_rl">
<TextView
android:id="@+id/txt_option_index"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/circle_shape_white_answer"
android:gravity="center"
android:text="A"
android:textColor="@color/black"
android:textSize="10dp" />
</RelativeLayout>
<TextView
android:id="@+id/optiontext"
android:layout_width="match_parent"
android:layout_marginTop="@dimen/topMarginU2"
android:layout_marginBottom="@dimen/topMarginU2"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/option_sign_bg_rl"
android:text="option one found"
android:layout_marginLeft="@dimen/topMarginU2"
android:clickable="true"
android:singleLine="false"
style="@style/txt_14_light_hex_grey_Text" />
</RelativeLayout>
我试过了:
ViewInteraction relativeLayout = onView(allOf(withId(R.id.txt_option_index),
withParent(withId(R.id.option_sign_bg_rl)),withText("B")));
relativeLayout.perform(scrollTo(), click());
和
ViewInteraction rr = onView(allOf(withId(R.id.txt_option_index),withText("A"))); rr.perform(click());
和
onView(allOf(withId(R.id.txt_option_index),hasSibling(withText("A")))).perform(click());
但没有运气, 错误
android.support.test.espresso.AmbiguousViewMatcherException
【问题讨论】:
-
尝试使用
onData()匹配器而不是onView。方法如下:stackoverflow.com/questions/29378552/… -
我试过
onData(),但没用。我也试过onPosition(),但没用,因为它不是具有位置值的gridview。在这种情况下,只有一个文本视图被多次使用来显示问题的选项。这些选项是动态创建的。
标签: android automated-tests android-espresso