Android Development Series (20): Functions and usage of AutoCompleteTextView (automatically completed text box,

Source: Internet
Author: User

Android Development Series (20): Functions and usage of AutoCompleteTextView (automatically completed text box,

After you enter a certain number of characters, the automatically completed text box displays a drop-down menu for you to choose from. After you select a menu item, AutoCompleteTextView can automatically enter the text box according to your selection.

AutoCompleteTextView is derived from EditText. It has one more function than the normal edit box: after a certain character is entered, the text is automatically completed.

Let's take a look at its attributes:

Android: completionHint: Set the title of the prompt in the drop-down menu

Android: completionHintView: Set the view with the title displayed in the drop-down menu

Android: dropDownHeight: Set the height of the drop-down menu

Android: dropDownHorizontalOffset: sets the horizontal cost between the drop-down menu and the text box. The drop-down menu is left aligned with the text box by default.

Android: dropDownVerticalOffset: sets the vertical offset between the drop-down menu and the text box. The drop-down menu is left aligned with the text box by default.

Android: dropDownWidth: Set the width of the drop-down menu

Android: popupleBackground: Set the background of the drop-down menu


In addition, AutonCompleteTextView derives from a subclass: MultiAutoCompleteTextView, which allows you to enter multiple prompt items. Each prompt item is separated by a separator.

MultiAutoCompleteTextView provides the setTokenizer () method to set separators.


First, create an Android project, and then write the main. xml file:

<Span style = "font-size: 14px;"> <? 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"> <! -- Define an Automatic completion text box, specify a character to enter and prompt --> <AutoCompleteTextView android: hint = "Please fill in your email address" android: id = "@ + id/auto" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: dropDownHorizontalOffset = "0dp" android: completionThreshold = "1"/> <! -- Define a MultiAutoCompleteTextView component --> <MultiAutoCompleteTextView android: id = "@ + id/mauto" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: completionThreshold = "1"/> </LinearLayout> </span>
Then, we compile the java code, AutoCompleteTextViewTest. java:

<Span style = "font-size: 14px;"> package org. crazyit. ui; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. arrayAdapter; import android. widget. autoCompleteTextView; import android. widget. button; import android. widget. multiAutoCompleteTextView; import android. widget. textView; public class AutoCompleteTextViewTest extends Activity {AutoCompleteTextView actv; MultiAutoCompleteTextView mauto; // define a String array as the text String of the prompt [] books = new String [] {"Big_AdamApple@qq.com ", "Big_AdamApple@baidu.com", "Big_AdamApple@gmail.com", "Big_AdamApple@sina.com", "Big_AdamApple@163.com", "Big_AdamApple@mail.com", "Big_AdamApple@11.com", };@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // create an ArrayAdapter that encapsulates the array ArrayAdapter <String> aa = new ArrayAdapter <String> (this, android. r. layout. simple_dropdown_item_1line, books); actv = (AutoCompleteTextView) findViewById (R. id. auto); // set Adapteractv. setAdapter (aa); mauto = (MultiAutoCompleteTextView) findViewById (R. id. mauto); // set Adaptermauto. setAdapter (aa); // set the separator mauto for MultiAutoCompleteTextView. setTokenizer (new MultiAutoCompleteTextView. commaTokenizer () ;}</span>

As follows:




In android: AutoCompleteTextView

Use the following code:
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, books );

In Android development, AutoCompleteTextView a is automatically completed in the text box. When asetSelectAllOnFocus (true);, a null pointer is thrown?

There is no new object, right? AutoCompleteTextView a = new AutoCompleteTextView (); try

Related Article

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.