Learn from scratch the Android Autocompleteview dynamically match the input content (like a search engine match)

Source: Internet
Author: User

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)

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.