Android Autocompletetextview Auto Prompt text box instance code _android

Source: Internet
Author: User
Tags gettext

Automatic prompt text box (Autocompletetextview) can enhance the user experience, shorten the user's input time (Baidu's search box is this effect).

First show you the effect of the picture, if you feel good, please refer to the implementation code:

The last one gets the value inside the text box (in fact, just like TextView, EditText):

First, define the Autocompletetextview control in XML:


 <linearlayout xmlns:android= "Http://" xmlns: tools= "Http://" android:layout_width= "match_parent" android:layout_height= "Match_parent" 
"android:gravity=" center "android:orientation=" vertical "> <autocompletetextview android:id=" @+id/actv_game " Android:layout_width= "220DP" android:layout_height= "wrap_content" android:completionhint= "@string/game_" Android: 
completionthreshold= "1" android:hint= "@string/game"/> <autocompletetextview android:id= "@+id/actv_car" Android:layout_width= "220DP" android:layout_height= "wrap_content" android:completionhint= "@string/car_" Android: completionthreshold= "1" android:hint= "@string/car"/> <button android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" android:layout_margin= "10DP" android:onclick= "GetValue" android:text= "@string/button "/> </linearlayout> 

The attribute completionhint is a message that is displayed to the user when the data is prompted:

android:completionhint= "@string/game_"

The property Completionthreshold is the starting position for the hint, and the default value is 2, which means that after entering two characters, the retrieval begins. The general setting is 1:

android:completionthreshold= "1"

Here are two Autocompletetextview, one gets the hint data from the XML, and the other takes the hint data from the collection.


<?xml version= "1.0" encoding= "Utf-8"?> <resources> <string name= "App_name" >actv_demo</string > <string name= "action_settings" >Settings</string> <string name= "Hello_world" >hello world!< /string> <string name= "Game" > Game </string> <string name= "car" > Vehicle </string> <string name= " Game_ > Please choose your favorite game </string> <string name= "Car_" > Please choose your favorite car </string> <string name= "button" > Gets the value of the text box </string> <string-array name= "Games" > <item> warcraft </item> <item> Warcraft 1</item> & 
lt;item> Warcraft 2</item> <item> Chinese paladin </item> <item> Paladin 1</item> <item> Paladin 2</item> 
<item>CS</item> <item>CS1</item> <item>CS2</item> <item>CF</item> <item>CF1</item> <item>CF2</item> <item>DNF</item> <item>dnf1</item > <item>DNF2</item> <item> Legends </item> 
<item> Legends 1</item> <item> legends 2</item> <item> World </item> <item> World 1</item > <item> World 2</item> </string-array> </resources>

The Games array is defined in the String.xml.

Package com.yx.actv_demo.ui; 
Import java.util.ArrayList; 
Import java.util.List; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.ArrayAdapter; 
Import Android.widget.AutoCompleteTextView; 
Import Android.widget.Toast; Import Com.yx.actv_demo. 
R /** * * This description is: the main interface * * @author: CS YX * @version: 1.0 * @date: 2014-10-24 PM 3:47:38 * * public class Mainactivity EX 
Tends activity {//Game text box private Autocompletetextview actv_game; 
Game text Box adapter private arrayadapter<charsequence> Gameadapter; 
Car private Autocompletetextview Actv_car; 
Private arrayadapter<string> Caradapter; Private list<string> cars;//collection data @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (s) 
Setcontentview (R.layout.activity_main); Initview ()//Initialize control InitData ()///Initialize data//instantiate adapter (fetch data from XML) Gameadapter = Arrayadapter.createfromresource (mainactivit Y.this,, Android. R. Layout.simple_spinner_item); 
The binding adapter displays data Actv_game.setadapter (gameadapter); The instantiation adapter takes the data from the array or collection Caradapter = new Arrayadapter<string> (Mainactivity.this, Android. 
R.layout.simple_spinner_item, cars); 
The binding adapter displays data Actv_car.setadapter (caradapter); 
/** * * This method describes: Gets the value of the text box * * @param view * View object/public void GetValue (view view) {String game = ""; 
if (actv_game!= null) {game = Actv_game.gettext (). toString ();//Get text box value} String car = ""; if (Actv_car!= null) {car = Actv_car.gettext (). toString ();//Get text box value} toast.maketext (Mainactivity.this, "Favorite game is:" + G 
Ame + "Like the car is:" + cars, Toast.length_long). Show (); 
/** * * This method describes: Initialize data/private void InitData () {cars = new arraylist<string> (); 
for (int i = 0; i < 5; i++) {Cars.add ("BMW-" + i); 
Cars.add ("Mercedes-Benz-" + i); 
Cars.add ("Hummer-" + i); 
Cars.add ("Land Rover-" + i); 
Cars.add ("Jeep-" + i); 
Cars.add ("Audi-" + i); 
Cars.add ("Ford-" + i); 
Cars.add ("Infiniti-" + i); }/** * * This method describes the initialization of the control */private void Initview () {actv_game = (Autocompletetextview) Findviewbyid (; 
Actv_car = (Autocompletetextview) Findviewbyid (; } 

First Arrayadapter instance:

Arrayadapter.createfromresource (context, Textarrayresid, textviewresid); 
Context Context object 
TEXTARRAYRESID data collection ID 
textviewresid Layout ID

Second Arrayadapter instance:

New Arrayadapter<string> (context, resource, objects); 
Context-Context Object 
resource Layout ID 
objects data collection

After instantiating Arrayadapter, Setadapter can!

Actv_game.setadapter (gameadapter); 
Actv_car.setadapter (Caradapter);

The above is a small set up to introduce the Android Autocompletetextview automatic prompt text box example code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.