Now we almost all use Baidu or Google to search for information on the Internet. When we enter one or two words in the input box, we will automatically prompt the information we want.AndroidHow is it implemented? In fact,AndroidOfAutocompletetextview widget.ArrayadapterIt can be designed likeGoogleSearch for the effect of the prompt.
In this exampleLayoutLayout oneAutocompletetextview widgetAnd then put this string array intoArrayadapter, And finally useAutocompletetextview. setadapterMethod, you can makeAutocompletetextviewProvides the automatic prompt function. For example, you only need to enterABWill automatically includeABList of all strings.
Let's take a look:
Below is ourProgramInvolved changesCode(The code in this example is rarely written ):
FirstMain. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Please input :"
/>
<Autocompletetextview
Android: Id = "@ + ID/ACTV"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</Linearlayout>
Second, the main control programAutocompletetextviewdemo. Java:
Package com. Android. test;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. arrayadapter;
Import Android. widget. autocompletetextview;
Public class autocompletetextviewdemo extends activity {
Private autocompletetextview ACTV;
Private Static final string [] autostrs = new string [] {"A", "ABC", "ABCD", "ABCDE", "ba "};
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Obtain ACTV through findviewbyid ()
ACTV = (autocompletetextview) findviewbyid (R. Id. ACTV );
// New arrayadapter object and pass the autostr string array to ACTV
Arrayadapter <string> adapter = new arrayadapter <string> (this,
Android. R. layout. simple_dropdown_item_1line, autostrs );
ACTV. setadapter (adapter );
}
}
All the programs are just a little bit. The success is achieved. The final execution will achieve the above results. Today is the end. Thank you !!!