【发布时间】:2016-10-22 09:21:45
【问题描述】:
我正在尝试测试 PreferenceScreen 上的复选框。
PreferenceScreen 包含两个 CheckBoxPreferences,每个都有唯一的 android:id。
<CheckBoxPreference
android:id="@+id/first_checkbox"
android:key="first_checkbox"
android:title="First checkbox" />
<CheckBoxPreference
android:id="@+id/second_checkbox"
android:key="second_checkbox"
android:title="Second checkbox" />
以下是 R.java 文件中的 id 值:
public static final class id {
public static final int first_checkbox=0x7f0a0000;
public static final int second_checkbox=0x7f0a0001;
}
在我的测试中:
ViewInteraction cbxFirst = onView(withId(R.id.first_checkbox));
我明白了:
NoMatchingViewException: No views in hierarchy found matching: with id:
com.test.fragmentpreference:id/first_checkbox
当我尝试通过“android.R.id.checkbox”而不是“R.id.first_checkbox”进行搜索时:
ViewInteraction cbxFirst = onView(withId(android.R.id.checkbox));
我收到:
AmbiguousViewMatcherException: 'with id: android:id/checkbox' matches multiple views in the hierarchy.
我的问题是:如何使用 'first_checkbox' id 测试第一个 CheckBoxPreference ?
【问题讨论】:
-
用
onView(withText("First checkbox"));之类的文字访问它怎么样? -
@Kamran Ahmed 我可以使用 'onView(withText("First checkbox"));' 访问它但我的目标是了解如何使用唯一的 android:id 访问特定视图,因为在许多情况下,当您有许多文本略有不同的视图时,使用“withText()”更容易出错。
-
我同意,我只是想更多地了解这个问题。在正常情况下它应该可以工作......我不确定
CheckBoxPreference的工作方式是否不同,理想情况下应该不会。
标签: android checkbox android-espresso checkboxpreference