Android editable drop-down box implementation

Source: Internet
Author: User

Implementation idea: click the button and pop up popupwindow under edittext. The view of popupwindow is filled with listview, and each item in listview is the data to be displayed.

Java code:

Package test. dropdown; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. layoutinflater; import android. view. motionevent; import android. view. view; import android. view. view. onclicklistener; import android. view. view. ontouchlistener; import android. view. viewgroup; import android. view. window; import android. Widget. baseadapter; import android. widget. edittext; import android. widget. imagebutton; import android. widget. listview; import android. widget. popupwindow; import android. widget. textview; public class testdorpdownactivity extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); setcontentview (R. layout. Dropdown); getviews (); adapter = new dropdownadapter (testdorpdownactivity. this, getdata (); Final listview = new listview (testdorpdownactivity. this); listview. setadapter (adapter); // textview // imgbtn. setonclicklistener (New onclicklistener () {public void onclick (view v) {// The constructor is written in the oncreate method body, and the width and height are not obtained because the layout is not loaded. If (null = pop) {// create a popuppop = new popupwindow (listview, layout. getwidth (), names. size () * layout. getheight (); pop. showasdropdown (layout);} else {If (pop. isshowing () {Pop. dismiss ();} else {Pop. showasdropdown (edittext) ;}}});} public list <string> getdata () {names = new arraylist <string> (); names. add ("10234124"); names. add ("88877111"); names. add ("ad93827166"); Return names ;}@ overrideprotected void onstop () {super. onstop (); pop. dismiss ();} public void getviews () {layout = findviewbyid (R. id. layout); imgbtn = (imagebutton) findviewbyid (R. id. imgbtn); edittext = (edittext) findviewbyid (R. id. edittext);}/** adapter used to display the popupwindow content */class dropdownadapter extends baseadapter {public dropdownadapter (context, list <string> List) {This. context = context; this. list = List;} public int getcount () {return list. size ();} public object getitem (INT position) {return NULL;} public long getitemid (INT position) {return position;} public view getview (final int position, view convertview, viewgroup parent) {layoutinflater = layoutinflater. from (context); convertview = layoutinflater. inflate (R. layout. list_row, null); close = (imagebutton) convertview. findviewbyid (R. id. close_row); content = (textview) convertview. findviewbyid (R. id. text_row); final string editcontent = List. get (position); content. settext (list. get (position ). tostring (); content. setontouchlistener (New ontouchlistener () {public Boolean ontouch (view V, motionevent event) {edittext. settext (editcontent); pop. dismiss (); Return false ;}}); close. setonclicklistener (New onclicklistener () {public void onclick (view v) {names. remove (position); adapter. notifydatasetchanged () ;}}); Return convertview;} private context; private layoutinflater; private list <string> list; private textview content; private imagebutton close;} private imagebutton imgbtn; private edittext; private list <string> names; private dropdownadapter adapter; private popupwindow pop; private view layout ;}

Layout file:

Dropdown. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "horizontal"> <relativelayout Android: id = "@ + ID/layout" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: paddingleft = "5dp" Android: paddingright = "5dp" Android: background = "@ drawable/search_box"> <textview Android: Id = "@ + ID/left_text" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_centervertical = "true" Android: layout_alignparentleft = "true" Android: textsize = "16sp" Android: gravity = "center" Android: text = "username:"/> <edittext Android: id = "@ + ID/edittext" Android: layout_width = "match_parent" Android: layout_height = "45dp" Android: layout_torightof = "@ ID/left_text" Android: layout_toleftof = "@ + ID/imgbtn" Android: layout_centervertical = "true" Android: Background = "@ Android: color/transparent"/> <imagebutton Android: id = "@ + ID/imgbtn" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Background = "@ Android: color/transparent" Android: layout_alignparentright = "true" Android: layout_centervertical = "true" Android: src = "@ drawable/login_more"/> </relativelayout> </linearlayout>

List_row.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >        <TextView         android:id="@+id/text_row"        android:layout_width="match_parent"        android:layout_height="45dp"        android:gravity="center_horizontal|center_vertical"        android:layout_weight="1"        />    <ImageButton         android:id="@+id/close_row"        android:layout_width="match_parent"        android:layout_height="45dp"        android:src="@drawable/delete_all"        android:background="@android:color/transparent"        android:layout_weight="5"        /></LinearLayout>

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.