Android self-paced auto-complete text box Autocompletetextview

Source: Internet
Author: User

The AutoComplete text box (Autocompletetextview) derives from EditText, and in fact he is an edit box, but he has one more function than the normal edit box: When the user enters a certain character, the AutoComplete text box displays a drop-down menu for the user to choose from. When a user selects a menu item, Autocompletetextview automatically fills in the text box as the user chooses.

Common XML attributes and related methods and descriptions supported by Autocompletetextview

XML properties Related methods Description
Android:completionhint Setcompletionhint (charsequence) Set the prompt title that appears in the drop-down menu
Android:completionthreshold Setthreshold (int) Set the user to enter at least a few words of blessing to display the prompt
Android:dropdownheight Setdropdownheight (int) Set the height of the drop-down menu
Android:dropdownhorizontaloffset Sets the horizontal offset between the drop-down menu and the text box, and the drop-down menu is left-aligned with the text box by default
Android:dropdownverticaloffset Sets the vertical offset between the drop-down menu and the text box, and the drop-down menu follows the text box by default
Android:dropdownwidth Setdropdownwidth (int) Set the width of the drop-down menu
Android:popupbackground Setdropdownbackgroundresource (int) Setting the background of a drop-down menu

The use of Autocompletetextview is actually very simple, as long as he set a adapter, the adapter encapsulates the autocompletetextview preset hint text, The following example describes an interface layout file that contains Autocompletetextview.

Layout/main.xml:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 android:orientation= "vertical"4 Android:layout_width= "Fill_parent"5 Android:layout_height= "Fill_parent"6     >7 <!--defines an AutoComplete text box, specifying that a character is entered to prompt -8 <Autocompletetextview9     Android:id= "@+id/auto"Ten Android:layout_width= "Fill_parent"  One Android:layout_height= "Wrap_content"  A Android:completionhint= "Please select the person you like:" - Android:dropdownhorizontaloffset= "20DP" - Android:completionthreshold= "1" the     /> - </LinearLayout>

Com.example.autocomplete.AutoCompleteTextViewTest.java

1  PackageCom.example.autocomplete;2 3 Importandroid.support.v7.app.ActionBarActivity;4 ImportAndroid.support.v7.app.ActionBar;5 Importandroid.support.v4.app.Fragment;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.LayoutInflater;8 ImportAndroid.view.Menu;9 ImportAndroid.view.MenuItem;Ten ImportAndroid.view.View; One ImportAndroid.view.ViewGroup; A ImportAndroid.widget.ArrayAdapter; - ImportAndroid.widget.AutoCompleteTextView; - ImportAndroid.os.Build; the  -  Public classAutocompletetextviewtestextendsactionbaractivity { -  -     //defines a string array, as the text of the hint +string[] Books =Newstring[]{ -"YangJing", +"Yangsen", A"Yanghua", at     }; -      - @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); in         //creates a arrayadapter, encapsulates an array -arrayadapter<string> arr =NewArrayadapter<string> ( This, Android. R.layout.simple_dropdown_item_1line, books); to          +Autocompletetextview Auto =(Autocompletetextview) Findviewbyid (R.id.auto); -                  the Auto.setadapter (arr); *     } $}

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.