Case study of AutoCompleteTextView auto-filling in Android learning notes
(1) There are two methods to implement the AutoCompleteTextView function: one is manually configured, another summary is the data made through the xml file (of course, it can also be obtained through online resources)
Here we only talk about the first two types!
(2) The layout Page code is the same as below:
(2) The first is to manually configure the list Data source:
Package com. example. autocompletetextview; public class MainActivity extends Activity {private AutoCompleteTextView autoCompleteTextView1; private ArrayAdapter
Adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); autoCompleteTextView1 = (AutoCompleteTextView) this. findViewById (R. id. autoCompleteTextView1);/** 1. manually list array adapter */adapter = new ArrayAdapter
(This, android. R. layout. simple_list_item_1, getDataSource (); autoCompleteTextView1.setAdapter (adapter);}/** 1. manually set a list array as the data source */public List
GetDataSource () {List
List = new ArrayList
(); 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 (yizhong); list. add (xindu); return list;} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
(4) The second type is the data source created through the xml file.
Name: Add the following configuration under the/values/strings file.
AutoCompleteTextView
Settings
Hello world!
Afghanistan
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Bfghanistan
Blbania
Blgeria
Bmerican Samoa
Bndorra
Bngola
Bnguilla
Bntarctica
Then the class is:
Package com. example. autocompletetextview; public class MainActivity extends Activity {private AutoCompleteTextView autoCompleteTextView1; private ArrayAdapter
Adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); autoCompleteTextView1 = (AutoCompleteTextView) this. findViewById (R. id. autoCompleteTextView1);/** 2. use the xml file to configure the data source */String [] countries = getResources () in the adapter (). getStringArray (R. array. countries_array); adapter = new ArrayAdapter
(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 running result is as follows: