Case study of AutoCompleteTextView auto-filling in Android learning notes

Source: Internet
Author: User

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:



 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.