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