android – 在uiautomator中从AutoCompleteTextView中选择项目

前端之家收集整理的这篇文章主要介绍了android – 在uiautomator中从AutoCompleteTextView中选择项目前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个自动化测试项目,其中有问题从AutoCompleteTextView中选择项目.

您可以看到捕捉和视图[全部展开]. AutoCompleteTextView的下拉菜单不会出现在视图树中,也可以使用鼠标进行选择.

我已经尝试下列方法从AutoCompleteTextView适配器中选择项目表单:

> UiScrollable locationList = new UiScrollable(new UiSelector().scrollable(true));
locationList.scrollTextIntoView(位置);
> UiScrollable locationList = new UiScrollable(locationEditText.getSelector());
locationList.scrollTextIntoView(位置);这里的locationEditText是我的AutoCompleteTextView
> UiObject selectedLocation = locationList.getChild(new UiSelector().text(location));
selectedLocation.click();从locationList,它不会选择带有字符串的项目.
> editLocationResId =“android:id / text1”;
UiObject selectedLocation = new UiObject(new UiSelector().resourceId(editLocationResId));
selectedLocation.click();来自adpter textview的id也不起作用.

任何人可以帮助我从uiautomator中的AutoCompleteTextView中选择项目?或者更多的方法得到欲望输出.

解决方法

尝试下面的代码为我工作.

我使用AutoCompleteText来自动完成用户当前所在的位置,locationList只不过是在strings.xml文件中写的数组,所以在这里使用你自己的字符串数组.

locationList = res.getStringArray(R.array.ticketLocation);

        ArrayAdapter<String> locationAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,locationList);

        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries);
        textView.setThreshold(1);
        textView.setAdapter(locationAdapter);
        textView.setValidator(new Validator());
        textView.setOnFocusChangeListener(new FocusListener());

        textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
                // TODO Auto-generated method stub
                TextView ticketLocation = (TextView) view;
                getTicketLocation = ticketLocation.getText().toString();
            }
        });

以下是用于验证位置字段中的文本输入的代码,fixText()方法阻止用户键入字符串数组中不存在的文本,例如:如果用户键入“germany”,不存在您的字符串数组列表将被替换为“”,它是编辑文本输入字段中的空字符串

class Validator implements AutoCompleteTextView.Validator {

        @Override
        public boolean isValid(CharSequence text) {
            // Log.v("Test","Checking if valid: " + text);
            Arrays.sort(locationList);
            if (Arrays.binarySearch(locationList,text.toString()) > 0) {
                return true;
            }

            return false;
        }

        @Override
        public CharSequence fixText(CharSequence invalidText) {
            // Log.v("Test","Returning fixed text");

            /*
             * I'm just returning an empty string here,so the field will be
             * blanked,but you could put any kind of action here,like popping
             * up a dialog?
             *
             * Whatever value you return here must be in the list of valid
             * words.
             */
            return "";
        }
    }

    class FocusListener implements View.OnFocusChangeListener {

        @Override
        public void onFocusChange(View v,boolean hasFocus) {
            // Log.v("Test","Focus changed");
            if (v.getId() == R.id.txtCountries && !hasFocus) {
                // Log.v("Test","Performing validation");
                ((AutoCompleteTextView) v).performValidation();
            }
        }
    }

猜你在找的Android相关文章