My android learning experience 12 and android learning experience 12
Automatically match the entered content. (If you have any questions at the end of the article, please answer them. Thank you)
The two controls are MultiAutoCompleteTextView and AutoCompleteTextView.
The main difference between the two controls and TextView is that they can automatically match the content entered by the user, just like Baidu. When entering information in the baidu search box, you will be prompted for some information.
The two controls have a property android: completionThreshold = "2 ", this attribute is mainly used to indicate the number of characters entered by the user to start matching (I am here two characters );
(1) First, let's talk about the implementation of AutoCompleteTextView.
The relevant code in the layout file is as follows (we all know the main attributes, and green is a unique attribute)
<AutoCompleteTextView
Android: id = "@ + id/autoCompleteTextView1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentRight = "true"
Android: layout_alignTop = "@ + id/textView1"
Android: hint = "Enter content"
Android: textColor = "#000000"
Android: completionThreshold = "2"
>
<RequestFocus/>
</AutoCompleteTextView>
The code in the source code file is
Private AutoCompleteTextView auTextView;
String [] res = {"ab1", "ab2", "ab3", "ab4", "ab5 "};
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
AuTextView = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView1 );
// Define the adapter and add the adapter
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. R. layout. simple_expandable_list_item_1, res );
AuTextView. setAdapter (adapter );
}
As follows:
(2) Implementation of MultiAutoCompleteTextView
The main difference between it and AutoCompleteTextView is that it can be matched multiple times, as long as the Delimiter is defined, while AutoCompleteTextView can only be matched once
Its code in the layout file is
<MultiAutoCompleteTextView
Android: id = "@ + id/multiAutoCompleteTextView1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_below = "@ + id/autoCompleteTextView1"
Android: layout_marginTop = "24dp"
Android: textColor = "#000000"
Android: completionThreshold = "2"
Android: hint = "Enter content"/>
Its code in the code file is:
Private MultiAutoCompleteTextView muTextView;
String [] res = {"ab1", "ab2", "ab3", "ab4", "ab5 "};
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// Define the adapter and add the adapter
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. R. layout. simple_expandable_list_item_1, res );
MuTextView = (MultiAutoCompleteTextView) findViewById (R. id. multiAutoCompleteTextView1 );
MuTextView. setAdapter (adapter );
// Set the comma delimiter
MuTextView. setTokenizer (new MultiAutoCompleteTextView. CommaTokenizer ());
}
As follows:
When I set the delimiter, I couldn't quite understand it. I checked it and used new MultiAutoCompleteTextView. CommaTokenizer (). I don't know why this is a comma separator. I hope you can answer this question. Thank you!
Please point out any errors. Thank you.