When the user enters a certain character, the AutoComplete text box can display a drop-down menu for the user to choose from, and when the user selects a menu item, Autocompletetextview can automatically fill in the text box as the user chooses
Autocompletetextview is derived from the EditText, a function more than the normal edit box: After entering a certain character, the text is automatically completed.
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 lower 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 allows multiple hint entries to be entered, separated by a delimiter between each hint item.
Multiautocompletetextview provides the Settokenizer () method to set the delimiter.
We start by creating a new Android project, and then you can 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 AutoComplete 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>
as follows:
Android Development Series (20): Autocompletetextview (Auto-complete text box) function and usage