Android learning notes: AutoCompleteTextView auto-filling case, android auto-Filling

Source: Internet
Author: User

Android learning notes: AutoCompleteTextView auto-filling case, android auto-Filling


(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:

<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: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <TextView android: id =" @ + id/textView1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentLeft =" true "android: layout_alignParentTop = "true" android: layout_marginTop = "34dp" android: text = "City:" android: textSize = "20dp"/> <AutoCompleteTextView android: id = "@ + id/autoCompleteTextView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBottom = "@ + id/textView1" android: layout_toRightOf = "@ + id/textView1" android: EMS = "10" android: text = ""> <requestFocus/> </AutoCompleteTextView> </RelativeLayout>


(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 <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 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> (); 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.

<?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>        <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 the xml file to configure the data source */String [] countries = getResources () in the adapter (). 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 running result is as follows:




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.