Automatic hint text box (Autocompletetextview) can enhance the user experience, shorten the user's input time (Baidu's search box is this effect).
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:gravi ty= "center" android:orientation= "vertical" > <autocompletetextview android:id= "@+id/actv_game" a Ndroid: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:completionh int= "@string/car_" android:completionthreshold= "1" android:hint= "@string/car"/> <button and Roid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margin= "10DP" an droid:onclick= "GetValue" android:text= "@String/button "/></linearlayout>
The Completionhint property is the prompt message that is displayed to the user when the data is prompted:
android:completionhint= "@string/game_"
The Completionthreshold property is the starting position of the prompt, and the default value is 2, which is to start the retrieval after entering two characters. The general setting is 1:
android:completionthreshold= "1"
There are two autocompletetextview, one fetching the cue data from the XML and the other fetching the cue 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" > Games </string> <string name= "car" > Car </string> <string name = "Game_" > Please select your favorite games </string> <string name= "Car_" > Please select your preferred car </string> <string name= "button" > Get the value of a text box </string> <string-array name= "Games" > <item> warcraft </item> <item> Warcraft 1& Lt;/item> <item> Warcraft 2</item> <item> Paladin </item> <item> Paladin 1</item> <item> Paladin 2</item> <item>CS</item> <item>CS1</item> <ITEM&G T cs2</item> <item>CF</item> <item>CF1</item> <item>cf2</item> ; <item>dnf</item&gT <item>DNF1</item> <item>DNF2</item> <item> Legends </item> <item> biography Chi 1</item> <item> Legends 2</item> <item> World </item> <item> World 1</item> <item> World 2</item> </string-array></resources>
Define the Games array in 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 class describes: main interface * * @author: CS YX * @version: 1.0 * @date: 2014-10-24 pm 3:47:38 */public Class Mainactivity extends 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 @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Initview ();//Initialize control InitData ();//Initialize data//instantiate adapter (Fetch data from XML) Gameadapter = Arrayadapter.createfromresource (Mainactivity.this,r.array.games, Android. R.layout.simple_spinner_item);//bind adapter displays data Actv_game.setadapter (Gameadapter);//The instantiation adapter takes data from an array or collection Caradapter = new Arrayadapter<string> (mainactivity.this,android. R.layout.simple_spinner_item, cars);//bind 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 (act V_game = null) {game = Actv_game.gettext (). toString ();//Gets the value of the text box}string car = ""; if (actv_car! = null) {car = Actv_car.get Text (). ToString ()//Gets the value of the text box}toast.maketext (Mainactivity.this, "Favorite game is:" + game + "like the car is:" + car,toast.length_long). Show ();} /** * * This method describes: Initialize data */private void InitData () {cars = new arraylist<string> (); for (int i = 0; i < 5; i++) {CA Rs.add ("BMW-" + i); Cars.add ("Mercedes-" + 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: Initialize the control */private void Initview () {actv_game = (Autocompletetextview) Findviewbyid (r.id.actv_game); Actv_ca R = (Autocompletetextview) Findviewbyid (r. Id.actv_car);}}
First instance of Arrayadapter:
Arrayadapter.createfromresource (context, Textarrayresid, textviewresid); Contextcontext Object Textarrayresid data collection Idtextviewresidlayout ID
A second Arrayadapter instance:
New Arrayadapter<string> (context, resource, objects); Contextcontext object Resourcelayout idobjects data collection
After instantiating Arrayadapter, Setadapter can!
Actv_game.setadapter (Gameadapter); Actv_car.setadapter (Caradapter);
Effect
The last one gets the value inside the text box (in fact, like TextView, EditText):
Android Auto prompt text box (autocompletetextview)