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.Android
How is it implemented? In fact,Android
OfAutocompletetextview widget
.Arrayadapter
It can be designed likeGoogle
Search for the effect of the prompt.
In this exampleLayout
Layout oneAutocompletetextview widget
And then put this string array intoArrayadapter
, And finally useAutocompletetextview. setadapter
Method, you can makeAutocompletetextview
Provides the automatic prompt function. For example, you only need to enterAB
Will automatically includeAB
List of all strings.
Let's take a look:
The following is the Code involved in the changes in our program (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 !!!