1.AutoCompleteTextView Properties:
1> dynamically matches the input content
2>android:completionthreshold= "n"--enter n characters to start matching
Add autocompletetextviewto Activity_main.xml:
The code is as follows:
<span style= "font-family:kaiti_gb2312;" ><autocompletetextview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:completionthreshold= "3"--enter n characters to start matching android:hint= "Please enter the keyword you want to search for" android:id= "@+id/ Autocompletetextview " android:layout_below=" @+id/textview " android:layout_alignparentleft=" true " Android:layout_alignparentstart= "true" /></span>
2. Specifically implemented in Maniactivity.class
First step: Initialize the control
Step Two: An adapter is required to fit the contents of the current text box input.
With a simple adapter Arrayadapter
Step three: Initialize the data Source = = = To match the content entered in the text box.
Eg:private string[]={,,};
Using generics
arrayadapter<string> adapter = new Arrayadapter<string> (,,);
Fourth step: Match the adapter with the current Autocompletetextview.
Actextview.steadapter (adapter);
The specific code is as follows:
<span style= "font-family:kaiti_gb2312;" >package Com.example.administrator.paoma;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.widget.ArrayAdapter;import Android.widget.autocompletetextview;public class Mainactivity extends Actionbaractivity {string[] guan = new String[]{ "Beijing1", "Beijing2", "Bejing", "Shanghai1", "Shanghai2", "Shnghai3"}; Private Autocompletetextview AtCo; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); /* * */AtCo = (Autocompletetextview) Findviewbyid (R.id.autocompletetextview); arrayadapter<string> adapter = new Arrayadapter<string> (this, Android. R.layout.simple_list_item_1,guan); Atco.setadapter (adapter); }}</span>
Explain:
<span style= "font-family:kaiti_gb2312;" > arrayadapter<string> adapter = New Arrayadapter<string> (this, Android. R.layout.simple_list_item_1,guan);</span>
<span style= "font-family:kaiti_gb2312;" > "This" means current, </span><pre name= "code" class= "java" ><span style= "font-family:kaiti_gb2312;" > "Android. R.layout.simple_list_item_1 "refers to the <span style=" FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX; > in Activity_main.xml </span><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >AutoCompleteTextView</span></span>
"Guan" refers to a data source that is automatically matched.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Learn from scratch the Android Autocompleteview dynamically match the input content (like a search engine match)