gpt4 book ai didi

android - Espresso 在布局中获取动态创建的子项

转载 作者:行者123 更新时间:2023-11-30 01:59:13 27 4
gpt4 key购买 nike

Button.java

public class Button extends FrameLayout {
public Button(Context context, AttributeSet attrs) {
super(context, attrs);
TextView textView = new TextView(context);
textView.setText("Test");
}
}

layout.xml

<LinearLayout>
<com.Button id="button_1" />
<com.Button id="button_2" />
</LinearLayout>

使用 Espresso,如何访问 Button 中创建的 TextView,并验证其文本?

onView(withId(R.id.button_1)<get_child>).check(matches(withText("Test")));

最佳答案

onView(withId(R.id.button_1)).check(matches(withChildText("Test")));

static Matcher<View> withChildText(final String string) {
return new BoundedMatcher<View, FrameLayout>(FrameLayout.class) {
@Override
public boolean matchesSafely(FrameLayout view) {
View child = view.getChildAt(0);
if (child != null && child instanceof TextView) {
return ((TextView) child).getText().toString().equals(string);
}
return false;
}

@Override
public void describeTo(Description description) {
description.appendText("with child text: ");
}
};
}

关于android - Espresso 在布局中获取动态创建的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755251/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com