Custom Control---System control combination (case three)

Source: Internet
Author: User

As follows


--------------------------------------------------

Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= ". Mainactivity ">    <edittext        android:id=" @+id/edittext1 "        android:layout_width=" 200dip "        android:layout_height= "Wrap_content"        android:layout_centerhorizontal= "true"        android:hint= "Please enter text"        Android:singleline= "true"/>    <imageview        android:id= "@+id/iv_down_arrow"        android:layout_ Width= "Wrap_content"        android:layout_height= "wrap_content"        android:layout_aligntop= "@+id/editText1"        android:layout_alignright= "@+id/edittext1"        android:src= "@drawable/down_arrow"/></ Relativelayout>

Popupwindow_item.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "> <imageview Andro        Id:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_centervertical= "true"        android:padding= "5dip" android:src= "@drawable/user"/> <textview android:id= "@+id/tv_msg" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_centerhorizontal= "        True "android:layout_centervertical=" true "android:text=" aaaaaaaa0 "android:textcolor=" #000000 " Android:textsize= "18sp"/> <imageview android:id= "@+id/iv_delete" android:layout_width= "Wrap_cont Ent "android:layout_height=" Wrap_content "android:layout_alignparentright=" true "Android:layout_cente Rvertical= "true" android:padding= "10dip" android:src= "@drawable/delete"/></relativelayout> 

Mainactivity.java

Package Com.atguigu.downarrow;import Java.util.arraylist;import Android.app.activity;import android.os.Bundle; Import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.listview;import Android.widget.popupwindow;import Android.widget.textview;public class MainActivity Extends Activity {private EditText edittext1;private ImageView iv_down_arrow;private Popupwindow window;// Custom Listviewprivate ListView listview;//data Private arraylist<string> msgs; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); editText1 = (EditText) Findviewbyid (R.ID.EDITTEXT1); iv_down_arrow = (ImageView) Findviewbyid (r.id.iv_down_arrow);// Set Click event-Eject Popuwindowiv_down_arrow.setonclicklistener (mClicklistener);//Prepare data msgs = new arraylist<string> (); for (int i =0;i<200;i++) {msgs.add ("aaaaaaaaaaa" +i);} Instantiate listview ListView = New ListView (this);//9patch picture Listview.setbackgroundresource (r.drawable.listview_ background);//Create and set adapter Myadaper adapter = new Myadaper (); Listview.setadapter (adapter);// Set click on a Listview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview< ?> parent, View view,int position, long id) {//1. Gets the content string msg = Msgs.get (position);//2. Set into the input box Edittext1.settext ( msg);//If you try to be not empty and try to be in the display state if (window!= null&&window.isshowing ()) {Window.dismiss ();//release resource window = NULL;}});} Private Onclicklistener Mclicklistener = new Onclicklistener () {@Overridepublic void OnClick (View v) {switch (V.getid ()) { Case r.id.iv_down_arrow://down arrow if (window = = null) {window = new Popupwindow (mainactivity.this); Window.setwidth ( Edittext1.getwidth ()); Window.setheight (n);//todo the ListView has not yet been created, one will come to continue this place to write code Window.setcontentview (ListView) ;//Set FocusWindow.setfocusable (TRUE);} Window.showasdropdown (editText1, 0, 0); break;default:break;}}};/ * * Create adapter */class myadaper extends baseadapter{@Overridepublic int GetCount () {return msgs.size ();} @Overridepublic Object getItem (int position) {return msgs.get (position);} @Overridepublic long Getitemid (int position) {return 0;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//Eventually return a viewview View; Viewholder holder;if (Convertview! = null) {view = Convertview;holder = (viewholder) View.gettag ();} else{//the layout file--->view object View = View.inflate (mainactivity.this, r.layout.popupwindow_item, null); holder = new Viewholder (); holder.tv_msg = (TextView) View.findviewbyid (r.id.tv_msg); holder.iv_delete = (ImageView) View.findviewbyid (r.id.iv_delete);//associated View.settag (holder);} The corresponding data is obtained based on the position of the final String msg = msgs.get (position); Holder.tv_msg.setText (msg); Holder.iv_delete.setOnClickListener ( New Onclicklistener () {@Overridepublic void OnClick (View v) {//1. Msgs.remove (msg) removed from the list;//2. BrushNew display notifydatasetchanged ();}}); return view;}} Static class Viewholder{textview Tv_msg;imageview Iv_delete;}}



Custom Control---System control combination (case three)

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.