The usage of AutoCompleteTextView and MultiAutoCompleteTextView in Android

  • 2020-06-01 10:58:54
  • OfStack

This paper lists the use methods of AutoCompleteTextView and MultiAutoCompleteTextView in Android with examples. The specific use methods are as follows:

First look at the use of AutoCompleteTextView:

Support basic autocomplete function, suitable for various search functions, and can set its default display data according to your own needs.
Both controls have the flexibility to pre-match that data, and to set how many values are entered to start matching, and so on.
The layout file is simple, as follows:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <AutoCompleteTextView
    android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

Note 1 that layout_width should not be set to wrap_content, otherwise the drop-down will only see the first hint, not the rest.
The business code is as follows:


protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 mTextView = (AutoCompleteTextView)findViewById(R.id.tv);
 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,autoStr);
 mTextView.setAdapter(adapter);
}

Use of MultiAutoCompleteTextView:

This control enables multiple values to be selected (in the case of multiple inputs), separated by delimiters, and automatically matched when each value is selected and re-entered.
It can be used to select contacts for text messages and emails.
You need to execute the set separator method when using.
MultiAutoCompleteTextView is similar to AutoCompleteTextView, except that you need to set the separator:
The specific usage method is added after the setAdapter () method:


mTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

Related articles: