(1) The adapter data source required to implement the Autocompletetextview function first is two methods, one result is manually configured, and the other is data made through XML file (which can also be obtained via online resources)
Only the first two types are spoken here!
(2) The layout of the page code is the same as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "an droid:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true "Android:layout_margintop=" 34DP "android:text=" City: "android:textsize=" 20DP "/> <autocompl Etetextview android:id= "@+id/autocompletetextview1" android:layout_width= "Wrap_content" android:layou t_height= "Wrap_content" ANDROID:LAYOUT_ALIGNBOttom= "@+id/textview1" android:layout_torightof= "@+id/textview1" android:ems= "ten" android:text= "" ; <requestfocus/> </AutoCompleteTextView></RelativeLayout>
(2) The first is a list data source that is manually configured:
Package Com.example.autocompletetextview;public class Mainactivity extends Activity {private Autocompletetextview Autocompletetextview1;private arrayadapter<string> adapter; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); AutoCompleteTextView1 = (Autocompletetextview) This.findviewbyid (R.ID.AUTOCOMPLETETEXTVIEW1);/* * 1. Use the manual list array adapter */adapter = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1, Getdatasource ()); Autocompletetextview1.setadapter (adapter);} /* * 1. Manually set a list array as the data source */public list<string> getdatasource () {list<string> list = new arraylist<string& gt; (); List.add ("Beijing"); List.add ("Beida"); List.add ("Beiren"); List.add ("Shanghai"); List.add ("Shangzhou"); List.add ("Guangzhou"), List.add ("Dancheng"), List.add ("ningping"), List.add ("Zhoukou"), List.add ("Chengdu"); List.add ("Heze"), List.add ("Zhumadian"), List.add ("Luoyang"), List.add ("Shuanglou"); List.add ("YizList.add ("Xindu"); return list;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
(4) The second is a data source made from an XML file
Name we need to add the below configuration under the/values/strings file
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "app_name" > "Autocompletetextview" </string> <string name= "action_settings" >Settings</string> <string name= "Hello_world" > Hello world!</string> <string-array name= "Countries_array" > <item>Afghanistan</item> <item>Albania</item> <item>Algeria</item> <item>american Samoa</item> ; <item>Andorra</item> <item>Angola</item> <item>Anguilla</item> &L T;item>antarctica</item> <item>Bfghanistan</item> <item>Blbania</item> <item>Blgeria</item> <item>bmerican samoa</item> <item>Bndorra</item> <item>Bngola</item> <item>Bnguilla</item> <item>bntarctica</item> ; </string-array></resources>
Then the class is:
Package Com.example.autocompletetextview;public class Mainactivity extends Activity {private Autocompletetextview Autocompletetextview1;private arrayadapter<string> adapter; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); AutoCompleteTextView1 = (Autocompletetextview) This.findviewbyid (R.ID.AUTOCOMPLETETEXTVIEW1);/* * 2. Use an XML file to configure the data source in the adapter */string[] countries = getresources (). Getstringarray (R.array.countries_array); adapter = new Arrayadapter<string> (this, Android. r.layout.simple_list_item_1,countries); Autocompletetextview1.setadapter (adapter);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
The results of the run are as follows:
Android Learning Note autocompletetextview Auto-fill case