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:
Activity_main.xml:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" 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.
Strings.xml
<?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.
Mainactivity.java:
Package com.yx.actv_demo.ui;
Import java.util.ArrayList;
Import java.util.List;
Import android.app.Activity;
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)
Avedinstancestate);
Setcontentview (R.layout.activity_main); Initview ()//Initialize control InitData ()///Initialize data//instantiate adapter (fetch data from XML) Gameadapter = Arrayadapter.createfromresource (mainactivit Y.this, R.array.games, 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 (r.id.actv_game);
Actv_car = (Autocompletetextview) Findviewbyid (R.id.actv_car); }
}
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!