Android-UI-AutoCompleteTextView

Source: Internet
Author: User

Preface

I have talked about EditText before. If you are interested, you can take a look. This blog mainly describes the text box that is automatically completed. It is actually a text editing box, which can be understood as an extension of the EditText function. It can prompt and automatically complete the input content. This article will explain how to set common attributes and how to add prompt data to AutoCompleteTextView. A demo will be used to demonstrate this.

 

Description

The AutoCompleteTextView text box is inherited from EditText. Therefore, it is actually a text editing box, but it only has the function of auto prompt input completion. Function similar to: After a user enters a certain character, the automatically completed text box displays a drop-down list for the user to choose from. After the user selects a menu item, autoCompleteTextView automatically fills in the text box according to the selection.

 

Common attributes

Because it is inherited from EditText, in addition to providing the Edit attributes and methods, AutoCompleteTextView also supports the following special attributes and Methods. Here we only introduce some commonly used attributes, for details, see the official documentation:

Android: completionHint/setCompletionHint (CharSequence): sets the title of the prompt displayed in the drop-down list.
Android: completionTjreshold/setThreshold (int): set to enter at least a few characters to display the prompt.
Android: dropDownHeight/setDropHeight (int): sets the height of the drop-down list.
Android: dropDownWidth/setDropWidth (int): Set the width of the drop-down list.
Android: popupBackground/setDropDownbackgroundResource (int): Set the background of the drop-down list.
 

Fill selected data

In Android, an Adapter interface is usually used to display data. Yes, this is an interface that connects back-end data and front-end display. It is an important link between data souce and UI (View. Demonstrate the relationship between the Adapter in the Android program:

 

For Adapter, it is an interface. Android declares various implementation classes for it. For the AutoCompleteTextView control, ArrayAdapter <T> is generally used to complete the function, for some application scenarios of other implementation classes, we will introduce them later.

ArrayAdapter <T> inherits from an abstract class BaseAdapter, which implements the Adapter interface. Therefore, the inheritance relationship should be: Adapter → BaseAdater → ArrayAdapter <T>.

From the name, we can see that ArrayAdapter <T> stores data in the form of an array. It does, and it can be constructed by passing an array. Therefore, we only need to fill in an array object to complete ArrayAdapter object initialization. After passing the obtained ArrayAdapter object to the AutoCompleteTextView control, we can select data settings for it.

MultiAutoCompleteTextView

Now that we have talked about AutoCompleteTextView, let's take a look at MultiAutoCompleteTextView, which inherits from AutoCompleteTextView. The new extended function is that you can perform multiple prompts and separate the specified content with symbols. To use MultiAutoCompleteTextView, you must implement a MultiAutoCompleteTextView. the Tokenizer interface is used to declare the symbols used to separate options. Generally, if this parameter is not specified, you can use the implementation class MultiAutoCompleteTextView provided by Android. commaTokenizer, which is set to use commas (,) for separation.

 

Demo

Next we will create an Android project to demonstrate the content mentioned above. We will provide two input boxes: AutoCompleteTextView and MultiAutoCompleteTextView, and then fill in the data of the famous city for demonstration, the details have been commented out in the code, and will not be explained separately here.

Layout file code:

1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical" 6 android: paddingBottom = "@ dimen/activity_vertical_margin" 7 android: paddingLeft = "@ dimen/plugin" 8 android: paddingRight = "@ dimen/activity_horizontal _ Margin "9 android: paddingTop =" @ dimen/activity_vertical_margin "10 tools: context = ". mainActivity "> 11 <TextView12 android: layout_width =" wrap_content "13 android: layout_height =" wrap_content "14 android: text =" @ string/autoText "/> 15 <! -- Declare an AutoCompleteTextView control, set the title of the drop-down box to "Famous Citi", and enter a character to start the prompt --> 16 <AutoCompleteTextView17 android: id = "@ + id/autotext" 18 android: completionHint = "Famous Citi" 19 android: completionThreshold = "1" 20 android: layout_width = "match_parent" 21 android: layout_height = "wrap_content"/> 2223 <TextView24 android: layout_width = "wrap_content" 25 android: layout_height = "wrap_content" 26 android: text = "@ string/multiautoText" 27/> 28 <MultiAutoCompleteTextView29 android: id = "@ + id/multiautotext" 30 android: layout_width = "match_parent" 31 android: layout_height = "wrap_content"/> 32 </LinearLayout>

Java code:

1 package com. bgxt. autocomplettextviewdemo; 2 3 import android. OS. bundle; 4 import android. app. activity; 5 import android. view. menu; 6 import android. widget. arrayAdapter; 7 import android. widget. autoCompleteTextView; 8 import android. widget. multiautocompletetext view; 910 public class MainActivity extends Activity {11 private AutoCompleteTextView autotext; 12 private MultiAutoCompleteTextView multiau Totext; 13 @ Override14 protected void onCreate (Bundle savedInstanceState) {15 super. onCreate (savedInstanceState); 16 setContentView (R. layout. activity_main); 17 18 // get the two control objects in the layout file 19 autotext = (AutoCompleteTextView) findViewById (R. id. autotext); 20 multiautotext = (MultiAutoCompleteTextView) findViewById (R. id. multiautotext); 21 22 // set the data source 23 String [] autoStrings = new String [] {"New York", "Tokyo", "beijing", "lond On "," Seoul Special "," Los Angeles "}; 24 // set the ArrayAdapter and display it in a single row drop-down list style (second parameter setting ). 25 ArrayAdapter <String> adapter = new ArrayAdapter <String> (MainActivity. this, 26 android. r. layout. simple_dropdown_item_1line, autoStrings); 27 // set Adapter28 autotext of AutoCompleteTextView. setAdapter (adapter); 29 30 // set the Adapter31 multiautotext of MultiAutoCompleteTextView. setAdapter (adapter); 32 // use commas to separate the set options. 33 multiautotext. setTokenizer (new MultiAutoCompleteTextView. CommaTokenizer (); 34} 35}

Effect:

 

 

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.