SoFunction
Updated on 2025-04-10

Usage of AutoCompleteTextView and MultiAutoCompleteTextView in Android

This article lists the usage methods of AutoCompleteTextView and MultiAutoCompleteTextView in Android using examples. The specific usage methods are as follows:

First lookUse of AutoCompleteTextView:

It supports basic automatic completion functions, is suitable for various search functions, and can set its default display data according to your needs.
Both controls can flexibly preset the matching data, and can set the functions of starting matching when entering how many values ​​are entered.
The layout file is simple, as follows:

<LinearLayout xmlns:andro
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <AutoCompleteTextView
    android:
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

Need to explain it herelayout_width should not be set to wrap_content, otherwise the first prompt can only be seen when the drop-down prompt is, and the content behind cannot be seen.
The business code is as follows:

protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_main);
 
 mTextView = (AutoCompleteTextView)findViewById();
 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, .simple_dropdown_item_1line,autoStr);
 (adapter);
}

Use of MultiAutoCompleteTextView:

This control can support selecting multiple values ​​(in the case of multiple inputs), separated by delimiters, and will automatically match when each value is entered again when it is selected.
You can choose the type of contact when sending text messages and emails.
When using it, you need to execute the method of setting the separator.
The use of MultiAutoCompleteTextView is similar to that of AutoCompleteTextView, but you need to set a separator
The specific usage method is to add it after the setAdapter() method:

(new ());