Android Learning Start-Autocompletetextview and Multiautocompletetextview controls using

Source: Internet
Author: User

When you search with Baidu, enter a few words on the drop-down box can be selected, the following two controls have this function

Autocompletetextview: Only the start of the input, only one match

Multiautocompletetextview: You can match multiple inputs, separating each input with a delimiter, just like when you send an email with multiple contacts, see example:

First, the effect of Autocompletetextview is as follows

The layout file is as follows

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7     8     <!--Property EMS indicates that a drop-down match box is displayed starting with the first word -9     <AutocompletetextviewTen         Android:id= "@+id/autocompletetextview1" One Android:layout_width= "Match_parent" A Android:layout_height= "Wrap_content" - Android:ems= "2"   - Android:hint= "Please enter keyword search" > the  -         <Requestfocus/> -     </Autocompletetextview> -  + </LinearLayout>

The code files are as follows

1  PackageCom.example.demo4;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.widget.ArrayAdapter;6 ImportAndroid.widget.AutoCompleteTextView;7 8  Public classMyactivitymainactivityextendsActivity {9     Ten     PrivateAutocompletetextview Actextview; One  A @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); -          -         //get to the control instance -Actextview =(Autocompletetextview) Findviewbyid (r.id.autocompletetextview1); +          -         //defines a string array to match the input keyword +string[] DSR =Newstring[] { A"Zhangshang", "Zhangshi", "Zhangzhang",  at"Lishang", "Ligang",  -"Chengcheng", "Chengzhi", -"Wangwu" -                 }; -          -         //Initializes a drop-down data source adapter to match the input inArrayadapter<string> adptr =NewArrayadapter<string>( -                  This,//Current Object toAndroid. R.layout.simple_list_item_1,//This is a system with its own layout +Dsr//Data Source -                 ); the                  *         //to set the Autocompletetextview control's matching device $ Actextview.setadapter (adptr);Panax Notoginseng          -     } the      +      A}

Second, the Multiautocompletetextview achieve the following results

Layout file is not on, it is a change of a control, directly look at the code, there is more than one statement

1  PackageCom.example.demo4;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.widget.ArrayAdapter;6 ImportAndroid.widget.MultiAutoCompleteTextView;7 8  Public classMyactivitymainactivityextendsActivity {9     Ten     PrivateMultiautocompletetextview Mactextview; One  A @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); -          -         //get to the control instance -Mactextview =(Multiautocompletetextview) Findviewbyid (r.id.multiautocompletetextview1); +          -         //defines a string array to match the input keyword +string[] DSR =Newstring[] { A"Zhangshang", "Zhangshi", "Zhangzhang",  at"Lishang", "Ligang",  -"Chengcheng", "Chengzhi", -"Wangwu" -                 }; -          -         //Initializes a drop-down data source adapter to match the input inArrayadapter<string> adptr =NewArrayadapter<string>( -                  This,//Current Object toAndroid. R.layout.simple_list_item_1,//This is a system with its own layout +Dsr//Data Source -                 ); the                  *         //to set the Autocompletetextview control's matching device $ Mactextview.setadapter (adptr);Panax Notoginseng          -         //device Multiple Input separator, here is a comma theMactextview.settokenizer (NewMultiautocompletetextview.commatokenizer ()); +          A     } the      +      -}

Finished, is not so easy,multiautocompletetextview than Autocompletetextview add a separate statement just, special is son inherited a more function!!!

Android Learning Start-Autocompletetextview and Multiautocompletetextview controls using

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.