Android getting started tutorial (12) imitation of Baidu Google search automatic prompt box -- autocompletetextview Application)

Source: Internet
Author: User

Now we almost all use Baidu or Google to search for information on the Internet. When we enter one or two words in the input box, we will automatically prompt the information we want.AndroidHow is it implemented? In fact,AndroidOfAutocompletetextview widget.ArrayadapterIt can be designed likeGoogleSearch for the effect of the prompt.

 

In this exampleLayoutLayout oneAutocompletetextview widgetAnd then put this string array intoArrayadapter, And finally useAutocompletetextview. setadapterMethod, you can makeAutocompletetextviewProvides the automatic prompt function. For example, you only need to enterABWill automatically includeABList of all strings.

 

Let's take a look:

 

 

Below is ourProgramInvolved changesCode(The code in this example is rarely written ):

 

FirstMain. 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>

Second, the main control programAutocompletetextviewdemo. 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 );

// Obtain ACTV through findviewbyid ()
ACTV = (autocompletetextview) findviewbyid (R. Id. ACTV );
// New arrayadapter object and pass the autostr string array to ACTV
Arrayadapter <string> adapter = new arrayadapter <string> (this,
Android. R. layout. simple_dropdown_item_1line, autostrs );
ACTV. setadapter (adapter );

}
}

 

All the programs are just a little bit. The success is achieved. The final execution will achieve the above results. Today is the end. Thank you !!!

Related Article

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.