Android Edittext+listpopupwindow implementation Editable drop-down list

Source: Internet
Author: User

Usage Scenarios

Autocompleteedittext the drop-down list only if you start typing and match the characters you have entered. The disadvantage of spinner is that it cannot be edited. So this article describes how to use the edittext+ Listpopupwindow implements an editable drop-down list. The usage scenario can be a login box with the Remember password feature.

Add up and down arrows to EditText

We need an arrow image, put it on the right side of the EditText, and click it to pop up the drop-down list.

To make this easy, just set a Drawableright property in the layout file for EditText.

<EditText        android:layout_width="match_parent"        android: Layout_height="wrap_content"        android:drawableright="  @drawable/pull_down1"/>
How to toggle the direction of arrows when clicked

Toggle the upper and lower arrows, in effect, is to reset the EditText drawableright. This requires the dynamic setting of the EditText drawableright.

Setcompounddrawables (left, top, right, bottom) and Setcompounddrawableswithintrinsicbounds (left, top, right, bottom);

This means that the settings drawable appear in the left, top, right, and bottom positions of text.

But there are some differences:
The width height of the drawable of Setcompounddrawables is set by Drawable.setbound (), so the drawables must already has had setbounds (Rect) Called. means that you must use Drawable.setbounds to set the length and width of the drawable before use.
And Setcompounddrawableswithintrinsicbounds is the drawable width of the painting is drawable fixed by the width of the high, that is, through the getintrinsicwidth () Obtained with Getintrinsicheight (), so only the drawables ' bounds will is set to their intrinsic bounds. That's the word!

For example:

NULL NULL null);

Add touch listening events to EditText, and re-set the image resources when you click to the Drawableright area

Edittext.setontouchlistener (NewView.ontouchlistener () {@Override PublicBoolean OnTouch (view view, motioneventEvent) {FinalintDrawable_left =0; FinalintDrawable_top =1; FinalintDrawable_right =2; FinalintDrawable_bottom =3; if(Event. getaction () = =motionevent.action_up) {                    if(Event. GetX () >= (Edittext.getwidth ()-EditText. Getcompounddrawables () [Drawable_right].getbounds (). Width ())) {                        //Your action hereEdittext.setcompounddrawableswithintrinsicbounds (NULL,NULL, Getresources (). Getdrawable (R.drawable.pull_up1),NULL); return true; }                }                return false; }        });
Listpopupwindow

The final step is to display the list.

Private voidShowlistpopulwindow () {string[] list= {"item1","item2","Item3","ITEM4" }; Listpopupwindow=NewListpopupwindow ( This); Listpopupwindow.setadapter (NewArrayadapter<string> ( This, Android.        R.layout.simple_list_item_1, list));        Listpopupwindow.setanchorview (EditText); Listpopupwindow.setmodal (true);    Listpopupwindow.show (); }

You can set a listener event for Listpopupwindow, what to do when item is selected, and what to do when Listpopupwindow disappears.
Listpopupwindow.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> adapterview, view view,intILongl) {edittext.settext (list[i]); }}); Listpopupwindow.setondismisslistener (NewPopupwindow.ondismisslistener () {@Override Public voidOndismiss () {edittext.setcompounddrawableswithintrinsicbounds (NULL,NULL, Getresources (). Getdrawable (R.DRAWABLE.PULL_DOWN1),NULL); }        });

Android Edittext+listpopupwindow implementation Editable drop-down list

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.