There is data as like

String asd[]={"A","B","C","AAA","BBB","CCC"}; 

I added them to the adapter and the adapter in AutoCompleteTextView

If you enter a symbol then a selection appears. But the trouble is that I want to add a few words. To be like that. A, BBB, AAA. But when I enter a character, the word drop check stops working because the dictionary doesn’t have the string "A,".

How to be?

  • @pavlofff, Tell me where did you come from so smart? What kind of article or golden book do you read about what you know about all the components? Give me this book pzhlsta) - Andro
  • 2
    here it is all written. - pavlofff

1 answer 1

In order to be able to enter several words through the separator with autocompletion of each, you need to use the MultiAutoCompleteTextView widget.

Example:

 private static final String[] COUNTRIES = new String[] {"Belgium", "France", "Italy", "Germany", "Spain"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.edit); textView.setAdapter(adapter); textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 

The widget needs to set the delimiter with the setTokenizer() method, which will separate the words entered with autocompletion. In the example, the separator is a ready-made comma-separated class - CommaTokenizer()

If the comma does not suit you as a separator, you can write your own class with the necessary separator (for example, ";"):

 public static class SemicolonTokenizer implements Tokenizer { public int findTokenStart(CharSequence text, int cursor) { int i = cursor; while (i > 0 && text.charAt(i - 1) != ';') { i--; } while (i < cursor && text.charAt(i) == ' ') { i++; } return i; } public int findTokenEnd(CharSequence text, int cursor) { int i = cursor; int len = text.length(); while (i < len) { if (text.charAt(i) == ';') { return i; } else { i++; } } return len; } public CharSequence terminateToken(CharSequence text) { int i = text.length(); while (i > 0 && text.charAt(i - 1) == ' ') { i--; } if (i > 0 && text.charAt(i - 1) == ';') { return text; } else { if (text instanceof Spanned) { SpannableString sp = new SpannableString(text + "; "); TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0); return sp; } else { return text + "; "; } } 

and then specify as separator:

 textView.setTokenizer(new SemicolonTokenizer()); 
  • ATP but I have long found the answer)). Just on the Android site. When will add the ability to close the topic if the answer is already found? - Andro
  • @xTIGRx The purpose of this resource is a knowledge base for many people, not personal advice. If the question and answer are qualitative, they do not need to be closed, many more people can use the solution, except for you. - pavlofff
  • Why then should I give a link to developer.android.com/develop/index.html . Do you give a complete answer to other people? After all, I on the developer.android.com/develop/index.html link completely found the answer to my question. You contradict yourself - Andro
  • @xTIGRx You asked where I find out, I showed you where. I do not see the connection between the existence of the offsite developer and this answer. - pavlofff