将Android Espresso typeText转换为ActionBar中的EditText [英] Android Espresso typeText into EditText in ActionBar

查看:53
本文介绍了将Android Espresso typeText转换为ActionBar中的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ActionBar中有一个SearchView,它是XML夸大的

I have a SearchView in my ActionBar that is inflated from XML

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_menu_search"
        android:showAsAction="always|collapseActionView"
        android:icon="@drawable/action_bar_search_icon"
        android:actionViewClass="android.widget.SearchView"/>
</menu>

我以这种方式在片段中对其进行充气:

I inflate it in my Fragment this way:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.search, menu);

    final MenuItem item = menu.findItem(R.id.action_menu_search);
    searchView = (SearchView) item.getActionView();
    searchView.setOnQueryTextListener(this);
}

我的测试:

onView(withId(R.id.catalog_filter_indicator_header_text)).perform(click());

//enter text and wait for suggestions
onView(withId(R.id.action_menu_search)).perform(click());
onViewisAssignableFrom(AutoCompleteTextView.class)).perform(click(), typeText("test"));

开始输入时,该字段看起来失去了焦点.而且我不知道为什么.

It looks like that field looses focus when it starts typing. And I don't know why.

找到所有视图,并且typeText语句顺利通过,但文本未出现在该字段中.我还尝试使用简单的EditText和自定义的android:actionLayout,但结果相同.

All views are found and typeText statement passes without a hitch, but text does not appear in the field. I also tried that with simple EditText and custom android:actionLayout but with the same results.

有什么我想念的吗?

推荐答案

最后,我决定创建自己的自定义操作,因为我无法进行typeText工作.这是我的代码:

Finally, I decided to create my own custom action because I wasn't be able to typeText work. Here you are my code:

public static ViewAction setText(final String text){
    return new ViewAction(){
        @Override
        public Matcher<View> getConstraints() {
            return allOf(isDisplayed(), isAssignableFrom(TextView.class));
        }

        @Override
        public String getDescription() {
            return "Change view text";
        }

        @Override
        public void perform(UiController uiController, View view) {
            ((TextView) view).setText(text);
        }
    };
}

此代码对于在TextView中的所有子代中编写代码很有用. F.例如EditText.

This code is useful to write code in all children from TextView. F. ex, EditText.

我从 https://stackoverflow.com/a/32850190/1360815

这篇关于将Android Espresso typeText转换为ActionBar中的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆