Android Development Series (20): Functions and usage of AutoCompleteTextView (automatically completed text box)
After you enter a certain number of characters, the automatically completed text box displays a drop-down menu for you to choose from. After you select a menu item, AutoCompleteTextView can automatically enter the text box according to your selection.
AutoCompleteTextView is derived from EditText. It has one more function than the normal edit box: after a certain character is entered, the text is automatically completed.
Let's take a look at its attributes:
Android: completionHint: Set the title of the prompt in the drop-down menu
Android: completionHintView: Set the view with the title displayed in the drop-down menu
Android: dropDownHeight: Set the height of the drop-down menu
Android: dropDownHorizontalOffset: sets the horizontal cost between the drop-down menu and the text box. The drop-down menu is left aligned with the text box by default.
Android: dropDownVerticalOffset: sets the vertical offset between the drop-down menu and the text box. The drop-down menu is left aligned with the text box by default.
Android: dropDownWidth: Set the width of the drop-down menu
Android: popupleBackground: Set the background of the drop-down menu
In addition, AutonCompleteTextView derives from a subclass: MultiAutoCompleteTextView, which allows you to enter multiple prompt items. Each prompt item is separated by a separator.
MultiAutoCompleteTextView provides the setTokenizer () method to set separators.
First, create an Android project, and then write the main. xml file:
Then, we compile the java code, AutoCompleteTextViewTest. java:
Package org. crazyit. ui; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. arrayAdapter; import android. widget. autoCompleteTextView; import android. widget. button; import android. widget. multiAutoCompleteTextView; import android. widget. textView; public class AutoCompleteTextViewTest extends Activity {AutoCompleteTextView actv; MultiAutoCompleteTextView mauto; // define a String array as the text String of the prompt [] books = new String [] {"Big_AdamApple@qq.com ", "Big_AdamApple@baidu.com", "Big_AdamApple@gmail.com", "Big_AdamApple@sina.com", "Big_AdamApple@163.com", "Big_AdamApple@mail.com", "Big_AdamApple@11.com", };@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // create an ArrayAdapter that encapsulates the array ArrayAdapter
Aa = new ArrayAdapter
(This, android. r. layout. simple_dropdown_item_1line, books); actv = (AutoCompleteTextView) findViewById (R. id. auto); // set Adapteractv. setAdapter (aa); mauto = (MultiAutoCompleteTextView) findViewById (R. id. mauto); // set Adaptermauto. setAdapter (aa); // set the separator mauto for MultiAutoCompleteTextView. setTokenizer (new MultiAutoCompleteTextView. commaTokenizer ());}}
As follows: