This example tells the Android imitation Baidu Google search automatic prompt box Autocompletetextview simple application. Share to everyone for your reference, specific as follows:
Now we almost all use the internet Baidu or Google search information, when we enter one or two words in the input box, will automatically prompt us the information we want, this effect on the Android is how to achieve it? In fact, Android's Autocompletetextview Widget, with Arrayadapter, can be designed with the same effect as Google search hints.
This example first lays out a autocompletetextview Widget in the layout and then puts the string array into Arrayadapter with a preset string array. Finally, using the Autocompletetextview.setadapter method, we can let Autocompletetextview have the function of automatic prompting. For example, if you enter AB, a list of all strings containing AB is automatically brought out.
Let's take a look at the effect chart:
Here is the code for the changes that are involved in our program (this example code is relatively small):
The first is main.xml:
<?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"
>
<textview
android:layout_width= "fill_parent"
android:layout_ height= "Wrap_content"
android:text= "Please input:"
/>
<autocompletetextview
android:id = "@+id/actv"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
/>
</LinearLayout>
The second is the main control program Autocompletetextviewdemo.java:
Package com.android.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.ArrayAdapter;
Import Android.widget.AutoCompleteTextView;
public class Autocompletetextviewdemo extends activity {
private autocompletetextview ACTV;
private static final string[] Autostrs = new string[]{"A", "abc", "ABCD", "ABCDE", "Ba"};
public void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
The ACTV
ACTV = (autocompletetextview) Findviewbyid (R.ID.ACTV) is taken from the Findviewbyid () method;
The new Arrayadapter object and passes the AUTOSTR string array into the ACTV
arrayadapter<string> adapter = new Arrayadapter<string> ( This,
Android. R.LAYOUT.SIMPLE_DROPDOWN_ITEM_1LINE,AUTOSTRS);
Actv.setadapter (adapter);
}
All the program is so little Oh, the success of the success of this, the final implementation, will achieve the above effect.
For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Layout Layout Skills Summary", "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course" , Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary" and "Android Control usage Summary"
I hope this article will help you with the Android program.