Android Development Series (20): Autocompletetextview (self-active completion of the text box) function and how to use

Source: Internet
Author: User

When the user enters a certain character, their own active completion of the text box can display a drop-down menu for the user to choose from, when the user selects a menu item, Autocompletetextview can voluntarily fill in the text box according to the user's choice

Autocompletetextview is derived from the edittext, more than the normal edit box a function: Enter a certain character, self-active completion of the text.

Below we look at its properties:

Android:completionhint: Set the tip title in the drop-down menu

Android:completionhintview: Set the view of the cue title in the drop-down menu

android:dropdownheight: Setting the height of the drop-down menu

Android:dropdownhorizontaloffset: Set the drop-down menu to a low level from 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, and the drop-down menu is left-aligned with the text box by default

android:dropdownwidth: Setting the width of the drop-down menu

Android:popuplebackground: Setting the background of the drop-down menu


In addition, Autoncompletetextview derives a subclass:Multiautocompletetextview, which agrees to enter multiple prompt entries, each of which is cut with a delimiter.

Multiautocompletetextview provides the Settokenizer () method to set the delimiter.


We start by creating a new Android project and then being able to write Main.xml files:

<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 a self-active text box, specifying that you enter a character to 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 write 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;//defines a string array, as the text of the hint string[] books = new string[]{"[email protected]", "[email  protected] "," [email protected] "," [email protected] "," [email protected] "," [email  Protected] "," [email protected] ",}; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.main);//Create a arrayadapter, encapsulate 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 delimiter for Multiautocompletetextview Mauto.settokenizer (new Multiautocompletetextview.commatokenizer ());}} </span>

For example, the following:



Android Development Series (20): Autocompletetextview (self-active completion of the text box) function and how to use

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.