Use of AutoCompleteTextView and MultiAutoCompleteTextView in Android

Source: Internet
Author: User

Android provides two types of smart input boxes: AutoCompleteTextView and MultiAutoCompleteTextView. Their functions are roughly the same. The display effect is the same as Google search. If you enter at least two characters in the search box, a drop-down box is displayed, prompting similar results.

The following describes in detail:

I. AutoCompleteTextView

1. Introduction:

An editable text view inherited from EditView can dynamically match the input content. For example, when google search engine inputs text, it can display matching hot information based on the content.

2. Important methods:

1) clearListSelection (): clears the selected list items.

2) dismissDropDown (): If the drop-down menu is closed

3) getAdapter (): Get the adapter

Ii. MultiAutoCompleteTextView

1. Introduction:

An editable text view inherited from AutoCompleteTextView can effectively expand the text you type without entering the entire content. If you enter a part of the content, the system prompts the remaining part of the content ).

You must provide a MultiAutoCompleteTextView. Tokenizer to distinguish different substrings.

2. Important methods:

1) enoughToFilter (): filter when the text length exceeds the threshold.

2) Validate mvalidation (): instead of verifying the entire text, this subclass method verifies each individual text mark

3) setTokenizer (MultiAutoCompleteTextView. Tokenizer t): When the user is inputting, The tokenizer setting is used to determine the range of text;


Here is a simple example:

1) MainActivity. java

Package com. example. l0914_5_autocompletetextview; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; import android. widget. autoCompleteTextView; import android. widget. multiAutoCompleteTextView; public class MainActivity extends Activity {private AutoCompleteTextView atv; private MultiAutoCompleteTextView matv; // declare the adapter private ArrayAdapter <String> adapter; // define the data source private String data [] = {"How to Learn Android", "What if you are sleepy in class", "how to lose weight quickly", "how to speed up computer Boot ", "The second season of haopin voice in China", "The English Group of haopin voice in China", "Use of SimpleCursorAdapter", "Use of SimpleAdapter", "Chen Xiao", "Chen xiaochun "}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); atv = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView1); matv = (MultiAutoCompleteTextView) findViewById (R. id. multiAutoCompleteTextView1); // initialize the adapter = new ArrayAdapter <String> (MainActivity. this, android. r. layout. simple_list_item_1, data); // adapt the adapter data source to AutoCompleteTextView atv. setAdapter (adapter); // It is automatically displayed when AutoCompleteTextView is set to 2 characters. The default value is also 2 ATVs. setThreshold (2); // adapt the adapter data source to MultiAutoCompleteTextView matv. setAdapter (adapter); matv. setThreshold (2); // setTokenizer matv. setTokenizer (new MultiAutoCompleteTextView. commaTokenizer ());}}

2) main_activity.xml

<LinearLayout 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: orientation = "vertical" tools: context = ". mainActivity "> <AutoCompleteTextView android: id =" @ + id/autoCompleteTextView1 "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: textColor =" #000000 "android: EMS = "10" android: hint = "Please input:"> <requestFocus/> </AutoCompleteTextView> <MultiAutoCompleteTextView android: id = "@ + id/multiAutoCompleteTextView1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: textColor = "#000000" android: EMS = "10" android: hint = "Enter: "/> </LinearLayout>

3) display effect:

Initial effect:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/10015T202-0.jpg "style =" float: none; "title =" Capture. JPG "/>

Enter China:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/10015R2F-1.jpg "style =" float: none; "title =" Capture 1.JPG"/>

Input Si:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/10015S629-2.jpg "style =" float: none; "title =" Capture 2.JPG"/>

Enter Chen Xiao:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/10015V3E-3.jpg "style =" float: none; "title =" Capture 3.JPG"/>


This article is from the MySpace blog, please be sure to keep this source http://wangzhaoli.blog.51cto.com/7607113/1293920

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.