As you can see from the name, both of these controls are used to enter information Textview,autocomplete have indicated that both control content input is automatically completed. The difference is that one is multi, one is not, the specific difference can be seen through the following content. The following describes the use of two controls, respectively.
Autocompletetextviewfunction
Dynamically matches the input content, such as when a search engine enters information into the input box, a drop-down list displays information about the current input.
Control-specific properties
Properties such as width, height, and so on are common properties of the control, and the properties specific to Autocompletetextview are described below:
-Android:completionthreshold: This property is used to set how many characters are entered when the control starts to automatically match
-Android:completionhint: Set the prompt title that appears in the drop-down menu
-Android:dropdownhorizontaloffset: The drop-down menu is offset horizontally between the text boxes. The default is left-aligned with the text box
-Android:dropdownheight: The height of the drop-down menu
-Android:dropdownwidth: Width of drop-down menu
-Android:singleline: Single-line display
-Android:dropdownverticaloffset: Vertical offset
as follows, the settings control starts to automatically match after entering 1 characters
<AutoCompleteTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:completionThreshold="1" />
The use of controls in activity
The use of Autocompletetextview in activity is divided into three steps:
1. Instantiate the control, that is, the control is found through Findviewbyid
2. Prepare the data source, data in the data source will match the input content, matching is displayed in the drop-down list
3. Set up the adapter, this step is like the ListView and other controls, we need the adapter to connect the data domain with the interface
Here is an example:
1. Create a project
2. Modify the Actiivty_main as follows:
<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/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"tools:context="Com.twotextview.MainActivity"> <autocompletetextview android:id="@+Id/tv_auto"Android:layout_width="Fill_parent"android:layout_height="Wrap_content"android:text=""Android:completionthreshold="1"/></relativelayout>
Code in 3.MainActivity:
PackageCom.twotextview;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.AutoCompleteTextView; Public class mainactivity extends actionbaractivity { PrivateAutocompletetextview Autocompletetextview;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//1. Instantiating a control This. Autocompletetextview = (Autocompletetextview) Findviewbyid (R.id.tv_auto);//2. Prepare the data source, which is the data displayed in the drop-down liststring[] Datas =Newstring[]{"Hello","Hay","Ha","World","Word","wa!"};//3. Set up the adapter to populate the control with dataarrayadapter<string> adapter =NewArrayadapter<string> ( ThisAndroid. R.layout.simple_list_item_1,datas); This. Autocompletetextview.setadapter (adapter); }}
4. The results of the operation are as follows:
Because we just do a simple demo, so in the choice of adapter, choose the simplest arrayadapter, if you want to display a combination of text, you can use Baseadapter or simpleadapter.
Multiautocompletetextviewfunction
You can select multiple values by separating them with separators, such as selecting a personal tag in the Contacts, personalization settings, etc. when sending a message.
When the first value is entered, it can automatically match, after the selection, enter the delimiter, when the input content, it can automatically match.
implementation Steps
- Instantiate the control, that is, the control is found through Findviewbyid
- Prepare the data source, the data in the data source will match the input content, the matching is displayed in the drop-down list
- Set up the adapter, like a control like a ListView, we need the adapter to connect the data field to the interface.
- Set the delimiter, you can create your own delimiter tokenizer, or you can use the internal default comma-delimited tokenizer
Here is an example:
1. Create the project (can also be modified on the basis of the previous project)
2. Modify the Activity_main file:
<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/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"tools:context="Com.twotextview.MainActivity"> <multiautocompletetextview android:id="@+Id/tv_multiauto"Android:layout_width="Fill_parent"android:layout_height="Wrap_content"android:text=""Android:completionthreshold="1"/></relativelayout>
3. Modify Mainactivity:
PackageCom.twotextview;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.AutoCompleteTextView;ImportAndroid.widget.MultiAutoCompleteTextView; Public class mainactivity extends actionbaractivity { PrivateMultiautocompletetextview Multiautocompletetextview;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//1. Instantiating a control This. Multiautocompletetextview = (Multiautocompletetextview) Findviewbyid (R.id.tv_multiauto);//2. Prepare the data source, which is the data displayed in the drop-down liststring[] Datas =Newstring[]{"Hello","Hay","Ha","World","Word","wa!"};//3. Set up the adapter to populate the control with dataarrayadapter<string> adapter =NewArrayadapter<string> ( ThisAndroid. R.layout.simple_list_item_1,datas); This. Multiautocompletetextview.setadapter (adapter);//4. Set the delimiter This. Multiautocompletetextview.settokenizer (NewMultiautocompletetextview.commatokenizer ()); }}
Which is different from the Autocompletetextview and only set the delimiter this step;
4. Operation Result:
After entering three entries, after entering the comma delimiter, you can start automatic matching.
Note: If you want to use your own custom delimiter, you need to write a class to inherit the Tokenizer interface, and then implement the method inside. Here you can see the source code of Multiautocompletetextview.commatokenizer () and then implement it yourself. Appendix The Source code:
/** * This simple tokenizer can is used for lists where the items is * separated by a comma and one or more s Paces. */ Public Static class Commatokenizer implements tokenizer { Public int Findtokenstart(charsequence text,intCursor) {inti = cursor; while(I >0&& Text.charat (i-1) !=', ') {i--; } while(I < cursor && Text.charat (i) = ="') {i++; }returnI } Public int Findtokenend(charsequence text,intCursor) {inti = cursor;intLen = Text.length (); while(I < Len) {if(Text.charat (i) = =', ') {returnI }Else{i++; } }returnLen } PublicCharsequenceTerminatetoken(charsequence text) {inti = Text.length (); while(I >0&& Text.charat (i-1) =="') {i--; }if(I >0&& Text.charat (i-1) ==', ') {returnText }Else{if(Textinstanceofspanned) {Spannablestring SP =NewSpannablestring (text +", "); Textutils.copyspansfrom ((spanned) text,0, Text.length (), Object.class, SP,0);returnSp }Else{returnText +", "; } } } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Android Basics" Autocompletetextview and Multiautocompletetextview