3

我需要验证是否选择了 TextView。这是对象:

TextView{
    id=2131230879,
    res-name=title,
    visibility=VISIBLE,
    width=101,
    height=144,
    has-focus=false,
    has-focusable=false,
    has-window-focus=true,
    is-clickable=false,
    is-enabled=true,
    is-focused=false,
    is-focusable=false,
    is-layout-requested=false,
    is-selected=true,
    root-is-layout-requested=false, 
    has-input-connection=false, 
    x=71.0, 
    y=0.0, 
    text=Size, 
    input-type=0, 
    ime-target=false
}

is-selected当您选择 textview 时,从 false 变为 true。

在 Espresso 中有内置的方法吗?

4

1 回答 1

2

想通了,我需要一个自定义匹配器来做到这一点,这是代码:

public static Matcher<View> isTextSelected() {
    return new TypeSafeMatcher<View>() {

        @Override
        public boolean matchesSafely(View view) {
            if (!(view instanceof TextView)) {
                return false;
            }
            return (view).isSelected();
        }
        @Override
        public void describeTo(Description description) {
            description.appendText("is-selected=true");
        }
    };
}

然后在视图上调用匹配器:onView(withText("XYZ")).check(matches(isTextSelected()));

于 2014-05-01T23:32:12.507 回答