从uiautomator中的AutoCompleteTextView中选择项目

Dea*_*ruv 11 automation android autocompletetextview android-uiautomator

用于选择项目的AutoCompleteTextView适配器问题

我正在创建一个自动化测试项目,我有问题从AutoCompleteTextView中选择项目.

您可以看到它的快照和视图[全部展开].AutoCompleteTextView的下拉列表不会出现在视图树中,也无法使用鼠标进行选择.

我尝试过以下方法从AutoCompleteTextView适配器中选择项目表单:

  1. UiScrollable locationList = new UiScrollable(new UiSelector().scrollable(true)); locationList.scrollTextIntoView(location);

  2. UiScrollable locationList = new UiScrollable(locationEditText.getSelector()); locationList.scrollTextIntoView(location); 这里的locationEditText是我的AutoCompleteTextView

  3. UiObject selectedLocation = locationList.getChild(new UiSelector().text(location)); selectedLocation.click(); 从locationList,它不会选择传递字符串的项目.

  4. editLocationResId = "android:id/text1"; UiObject selectedLocation = new UiObject(new UiSelector().resourceId(editLocationResId)); selectedLocation.click(); 来自adpter textview的id也不起作用.

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

小智 -1

尝试下面的代码,它对我有用。

我正在使用 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();
            }
        });
Run Code Online (Sandbox Code Playgroud)

下面是验证位置字段中文本输入的代码,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();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的努力。但请仔细阅读问题,然后尝试发表评论或回答。如果您不明白问题,可以询问。[(PS)这是Automator测试代码] (2认同)