Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中使用Espresso为textview设置值_Android_Unit Testing_Textview_Android Espresso - Fatal编程技术网

如何在Android中使用Espresso为textview设置值

如何在Android中使用Espresso为textview设置值,android,unit-testing,textview,android-espresso,Android,Unit Testing,Textview,Android Espresso,我正在为片段编写测试用例。我可以将文本设置为Edittext,但无法从测试中将文本设置为文本视图。请大家帮我解决这个问题。下面是我编辑文本的代码 onView(withId(R.id.editText)) .perform(typeText("my text"), closeSoftKeyboard()); 请帮助我如何将文本设置为文本视图。这是由于i.e 查看前提条件: 必须显示在屏幕上 必须支持输入法 TextView(用户无法通过键盘或其他方式向其输入值)不支持,因

我正在为片段编写测试用例。我可以将文本设置为Edittext,但无法从测试中将文本设置为文本视图。请大家帮我解决这个问题。下面是我编辑文本的代码

   onView(withId(R.id.editText))
        .perform(typeText("my text"), closeSoftKeyboard());

请帮助我如何将文本设置为文本视图。

这是由于i.e

查看前提条件:

必须显示在屏幕上

必须支持输入法

TextView
(用户无法通过键盘或其他方式向其输入值)不支持,因此存在问题

解决方案:您可以实现自己的视图
ViewAction


在视图中,这是什么?动作allOf,可从中指定。这是为了类型安全。i、 e.找到的视图应该是textview的子类型,有点像checkis的实例。两种方法都可以导入,但无法改进该方法。添加
导入静态android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom
public static ViewAction setTextInTextView(final String value){
            return new ViewAction() {
                @SuppressWarnings("unchecked")
                @Override
                public Matcher<View> getConstraints() {
                    return allOf(isDisplayed(), isAssignableFrom(TextView.class));
//                                            ^^^^^^^^^^^^^^^^^^^ 
// To check that the found view is TextView or it's subclass like EditText
// so it will work for TextView and it's descendants  
                }

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

                @Override
                public String getDescription() {
                    return "replace text";
                }
            };
    }
onView(withId(R.id.editText))
        .perform(setTextInTextView("my text"));