The AutoComplete text box (Autocompletetextview) derives from EditText, and in fact he is an edit box, but he has one more function than the normal edit box: When the user enters a certain character, the AutoComplete text box displays a drop-down menu for the user to choose from. When a user selects a menu item, Autocompletetextview automatically fills in the text box as the user chooses.
Common XML attributes and related methods and descriptions supported by Autocompletetextview
XML properties |
Related methods |
Description |
Android:completionhint |
Setcompletionhint (charsequence) |
Set the prompt title that appears in the drop-down menu |
Android:completionthreshold |
Setthreshold (int) |
Set the user to enter at least a few words of blessing to display the prompt |
Android:dropdownheight |
Setdropdownheight (int) |
Set the height of the drop-down menu |
Android:dropdownhorizontaloffset |
|
Sets the horizontal offset between the drop-down menu and the text box, and 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, and the drop-down menu follows the text box by default |
Android:dropdownwidth |
Setdropdownwidth (int) |
Set the width of the drop-down menu |
Android:popupbackground |
Setdropdownbackgroundresource (int) |
Setting the background of a drop-down menu |
The use of Autocompletetextview is actually very simple, as long as he set a adapter, the adapter encapsulates the autocompletetextview preset hint text, The following example describes an interface layout file that contains Autocompletetextview.
Layout/main.xml:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 android:orientation= "vertical"4 Android:layout_width= "Fill_parent"5 Android:layout_height= "Fill_parent"6 >7 <!--defines an AutoComplete text box, specifying that a character is entered to prompt -8 <Autocompletetextview9 Android:id= "@+id/auto"Ten Android:layout_width= "Fill_parent" One Android:layout_height= "Wrap_content" A Android:completionhint= "Please select the person you like:" - Android:dropdownhorizontaloffset= "20DP" - Android:completionthreshold= "1" the /> - </LinearLayout>
Com.example.autocomplete.AutoCompleteTextViewTest.java
1 PackageCom.example.autocomplete;2 3 Importandroid.support.v7.app.ActionBarActivity;4 ImportAndroid.support.v7.app.ActionBar;5 Importandroid.support.v4.app.Fragment;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.LayoutInflater;8 ImportAndroid.view.Menu;9 ImportAndroid.view.MenuItem;Ten ImportAndroid.view.View; One ImportAndroid.view.ViewGroup; A ImportAndroid.widget.ArrayAdapter; - ImportAndroid.widget.AutoCompleteTextView; - ImportAndroid.os.Build; the - Public classAutocompletetextviewtestextendsactionbaractivity { - - //defines a string array, as the text of the hint +string[] Books =Newstring[]{ -"YangJing", +"Yangsen", A"Yanghua", at }; - - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); in //creates a arrayadapter, encapsulates an array -arrayadapter<string> arr =NewArrayadapter<string> ( This, Android. R.layout.simple_dropdown_item_1line, books); to +Autocompletetextview Auto =(Autocompletetextview) Findviewbyid (R.id.auto); - the Auto.setadapter (arr); * } $}