Android auto-match text AutoCompleteTextView/MultiAutoCompleteTextView, androidtextview class
AutoCompleteTextView: each text box can only match once
MultiAutoCompleteTextView: Multiple matching by Separators
1 // AutoComplete XML Example 2 <AutoCompleteTextView 3 android: completionThreshold = "3" // when the number of inputs reaches 3, the prompt 4 android: id = "@ + id/autoCompleteTextView1" 5 android: layout_width = "match_parent" 6 android: layout_height = "wrap_content" 7 android: layout_alignParentLeft = "true" 8 android: layout_alignParentTop = "true" 9 android: EMS = "10" 10 android: hint = "Enter keywords"> 11 </AutoCompleteTextView>
1 // AutoComplete java Example 2 3 public class MainActivity extends Activity {4 private AutoCompleteTextView acTextView; 5 private String [] res = {"beijing1", "beijing2", "beijing3 ", // message indicating resource 6 "shanghai1", "shanghai2"}; 7 8 @ Override 9 protected void onCreate (Bundle savedInstanceState) {10 super. onCreate (savedInstanceState); 11 setContentView (R. layout. fragment_main); 12/** 13*1. initialize the control 14*2. An adapter is required, arrayAdapter <String> 15*3. initialize the data source --- match the content entered in the text box 16*4. Bind the adapter to AutoCompleteTextView 17 */18 19 acTextView = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView1); 20 ArrayAdapter <String> adapter = new ArrayAdapter <> (this, 21 android. r. layout. simple_list_item_1, res); // three parameters: context, layout, resource 22 acTextView. setAdapter (adapter); // bind 23} 24}
1 // MultiAutoCompleteTextView XML Example 2 3 <MultiAutoCompleteTextView 4 android: id = "@ + id/multiAutoCompleteTextView1" 5 android: layout_width = "match_parent" 6 android: layout_height = "wrap_content" 7 android: layout_alignParentLeft = "true" 8 android: layout_alignParentTop = "true" 9 android: EMS = "10" 10 android: hint = "enter a keyword"/>
1 // MultiAutoCompleteTextView java Example 2 3 public class MainActivity extends Activity {4 private MultiAutoCompleteTextView macTextView; 5 private String [] res = {"beijing1", "beijing2", "beijing3 ", 6 "shanghai1", "shanghai2"}; 7 8 @ Override 9 protected void onCreate (Bundle savedInstanceState) {10 super. onCreate (savedInstanceState); 11 setContentView (R. layout. fragment_main); 12/** 13*1. initialize the control 14*2. An adapter is required, arrayAdapter <String> 15*3. initialize the data source --- match the content entered in the text box 16*4. Bind the adapter to AutoCompleteTextView 17*5. Set the delimiter 18 */19 20 macTextView = (multiAutoCompleteTextView) findViewById (R. id. multiAutoCompleteTextView1); 21 ArrayAdapter <String> adapter = new ArrayAdapter <> (this, 22 android. r. layout. simple_list_item_1, res); 23 macTextView. setAdapter (adapter); 24 // you can specify 25 macTextView as a comma. setTokenizer (new MultiAutoCompleteTextView. commaTokenizer (); 26 // In the custom system, only comma is used as the separator, and other separators must be set as 27} 28}
2014-08-16
In android: AutoCompleteTextView
Use the following code:
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, books );
Android development: solution to the problem that AutoCompleteTextView can be completed only after two characters are entered
Android provides a textview with the automatic prompt function. You can write several lines of code according to the built-in documents and apidemo to implement this function. However, the default thresh value is 2. Therefore, you must enter two or more characters by default to enable the automatic prompt function. Otherwise, AutoCompleteTextView will not receive any prompts. Of course, you can use setThresh to set a prompt after entering at least a few characters, or you can set it in xml. AutoCompleteTextView matches all your strings as substrings, while MultiAutoCompleteTextView breaks down the strings you have already entered Based on the tokenizer you provided, the last few characters that meet the conditions are automatically prompted as substrings.
For example, if you set MultiAutoCompleteTextView. setTokenizer (newMultiAutoCompleteTextView. CommaTokenizer ());
When you enter It, It will prompt you Italy, and then you choose this string. In this case, the string in textview is "Italy", followed by ", It". In this case, the string is Italy in textview, It, in this case, MultiAutoCompleteTextView breaks down the "It" according to the configured Word Segmentation Method (CommaTokenizer) and matches It again. The prompt Italy is displayed. this is the origin of Multi. Supports multiple word prompts. Of course, you must set the correct Tokenizer ).