Autocompletetextview function is similar to Baidu or Google in the search bar when entering information, pop-up and input information close to the prompt information. Of course, there are some adapters to use here.
Two smart input boxes are available in Android, which are Multiautocompletetextview, Autocompletetextview. They function roughly the same, and the difference between it and Autocompletetextview is that Multiautocompletetextview can always add new selection values in the input box. The writing method is also different, and you need to call Settokenizer () after Setadapter. Here is a detailed description.
First, Autocompletetextview
1. Introduction
An editable text view that pops up when the user enters information. The hint list is displayed in a drop-down menu from which the user can select an item to complete the input. The hint list is the data obtained from a data adapter.
2. Important Methods
Clearlistselection (): Clears the selected list item
Dismissdropdown (): If there is a close drop-down menu
Getadapter (): Get Adapter
3. Notice of Creation
(1) Layout file
<autocompletetextview android:id= "@+id/edit" android:layout_width= "match_parent" android:layout _height= "Wrap_content"/>
(2) procedure
Instantiating an adapter
arrayadapter<string> adapter = new Arrayadapter<string> (this,android. R.layout.simple_dropdown_item_1line, STRs);
Setting up the adapter
Edit.setadapter (adapter);
Second, Multiautocompletetextview
1. Introduction
Inheriting from Autocompletetextview, extending the length of the Autocompletetextview, you have to provide a multiautocompletetextview.tokenizer to differentiate the different substrings
2. Important Methods
Enoughtofilter (): Filter When text length exceeds threshold
Performvalidation (): Instead of validating the entire text, this subclass method validates each individual text tag
Settokenizer (Multiautocompletetextview.tokenizer t); Tokenizer settings will be used to determine the text-related range when the user is typing
3. Notice of Use
(1) Layout file
<multiautocompletetextview android:id= "@+id/edit1" android:layout_width= "Match_parent" android: layout_height= "Wrap_content"/>
(2) procedure
Instantiating an adapter
arrayadapter<string> adapter = new Arrayadapter<string> (this,android. R.layout.simple_dropdown_item_1line, STRs);
Determine scope
Edit1.settokenizer (New Multiautocompletetextview.commatokenizer ())
Here's an example.
Autocommittest.java
Package Org.hualang.auto; Import android.app.Activity; Import Android.os.Bundle; Import Android.widget.ArrayAdapter; Import Android.widget.AutoCompleteTextView; Import Android.widget.MultiAutoCompleteTextView; public class Autocommittest extends activity {/** Called when the activity is first created. */Private STA TIC final string[] autostring=new string[]{"Welcome", "Well", "Weatch", "Weexeview", "Werap"}; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Associate Keyword arrayadapter<string> adapter=new arrayadapter<string> (this, Android. R.layout.simple_dropdown_item_1line, autostring); Autocompletetextview autocomplete= (Autocompletetextview) Findviewbyid (R.id.auto); Autocomplete.setadapter (adapter); Multiautocompletetextview multi= (MultiautocompLetetextview) Findviewbyid (R.id.multi); Add Adapter to Autocompletetextview Multi.setadapter (adapter); Multi.settokenizer (New Multiautocompletetextview.commatokenizer ()); } }
Main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:orientation=" vertical " android:layout_width=" fill_parent " android:layout_ height= "Fill_parent" > <textview android:layout_width= "fill_parent" android:layout_ height= "Wrap_content" android:text= "Auto Prompt function Demo" /> <autocompletetextview android:id= "@+ Id/auto " android:layout_width=" fill_parent " android:layout_height=" wrap_content " /> <multiautocompletetextview android:id= "@+id/multi" android:layout_width= "Fill_parent " android:layout_height= "Wrap_content" /> </LinearLayout>
The results of the operation are as follows:
The above is the Android UI Control series: Autocompletetextview (Auto prompt) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!