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文件中写的数组,所以在这里使用你自己的字符串数组.

  1. locationList = res.getStringArray(R.array.ticketLocation);
  2.  
  3. ArrayAdapter<String> locationAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,locationList);
  4.  
  5. AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries);
  6. textView.setThreshold(1);
  7. textView.setAdapter(locationAdapter);
  8. textView.setValidator(new Validator());
  9. textView.setOnFocusChangeListener(new FocusListener());
  10.  
  11. textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  12. @Override
  13. public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
  14. // TODO Auto-generated method stub
  15. TextView ticketLocation = (TextView) view;
  16. getTicketLocation = ticketLocation.getText().toString();
  17. }
  18. });

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

  1. class Validator implements AutoCompleteTextView.Validator {
  2.  
  3. @Override
  4. public boolean isValid(CharSequence text) {
  5. // Log.v("Test","Checking if valid: " + text);
  6. Arrays.sort(locationList);
  7. if (Arrays.binarySearch(locationList,text.toString()) > 0) {
  8. return true;
  9. }
  10.  
  11. return false;
  12. }
  13.  
  14. @Override
  15. public CharSequence fixText(CharSequence invalidText) {
  16. // Log.v("Test","Returning fixed text");
  17.  
  18. /*
  19. * I'm just returning an empty string here,so the field will be
  20. * blanked,but you could put any kind of action here,like popping
  21. * up a dialog?
  22. *
  23. * Whatever value you return here must be in the list of valid
  24. * words.
  25. */
  26. return "";
  27. }
  28. }
  29.  
  30. class FocusListener implements View.OnFocusChangeListener {
  31.  
  32. @Override
  33. public void onFocusChange(View v,boolean hasFocus) {
  34. // Log.v("Test","Focus changed");
  35. if (v.getId() == R.id.txtCountries && !hasFocus) {
  36. // Log.v("Test","Performing validation");
  37. ((AutoCompleteTextView) v).performValidation();
  38. }
  39. }
  40. }

猜你在找的Android相关文章