"Andrioid_popupwindow case" Popwindow do Menu,popwindow to do options menu

Source: Internet
Author: User

First look at the results, showing the most recently developed projects often used in the pop case, including General options, drop-down options, menu, etc., the layout is not posted, very simple

Believe can be seconds to understand, paste the core code of pop

/** * * @author Yaguang.wang * */public class Menupop extends Popupwindow {private int resid;private Context MCONTEXT;PRI Vate View popview;private layoutinflater inflater;public menupop (int resid,context context) {super (context); This.resid = Resid;mcontext = context;inflater= (layoutinflater) Mcontext.getsystemservice (Context.layout_inflater_service); Init ();} @SuppressWarnings ("deprecation") public void init () {Popview = inflater.inflate (This.resid, NULL); Popview.setlayoutparams (New Layoutparams (Layoutparams.match_parent, layoutparams.wrap_content)); SetContentView ( Popview); SetWidth (layoutparams.fill_parent); SetHeight (layoutparams.wrap_content); Setbackgrounddrawable (new Colordrawable (color.transparent)); Setfocusable (true); Button btn1 = (button) Popview.findviewbyid (R.ID.MENU_BTN1); Button btn2 = (button) Popview.findviewbyid (R.ID.MENU_BTN2); Button Btn3 = (button) Popview.findviewbyid (R.ID.MENU_BTN3); Button Btn4 = (button) Popview.findviewbyid (R.ID.MENU_BTN4); Btn1.setonclicklistener (New ButtOnlistener ()); Btn2.setonclicklistener (new Buttonlistener ()); Btn3.setonclicklistener (new Buttonlistener ()); Btn4.setonclicklistener (New Buttonlistener ());p Opview.setfocusableintouchmode (True);p Opview.setonkeylistener ( New View.onkeylistener () {@Overridepublic Boolean onKey (View v, int keycode, keyevent event) {if (keycode = = Keyevent.keyco De_menu && isshowing ()) {dismiss (); return true;} return false;}}); Private class Buttonlistener implements view.onclicklistener{@Overridepublic void OnClick (View v) {Toast.maketext ( Mcontext, "View ID is" +v.getid (), Toast.length_short). Show (); if (isshowing ()) {Dismiss ();}}}

Above is the source code of Menupop. Can realize pop press the menu key can pop up, again the menu key disappears, click Other areas, or press the return button, or pop view has a click event when pop disappears. Here the specific reference to http://blog.csdn.net/admin_/article/details/7278402 this article, this article is well written. In the beginning, I met a pop bounce that didn't go away,

Setfocusable (TRUE);

Popview.setfocusableintouchmode (TRUE);

These two methods are critical. Pop is turned into a zombie view after it pops up, but the view inside the pop can still receive events, but it's time to pass the focus to the pop view, so view is responsive to the onkeydown event and the onclick event. Problem solved.

/** * * @author Yaguang.wang * */public class Gridpop extends Popupwindow {private Context context;private Layoutinflater Layoutinflater;public View allview;private int resid;private GridView allitemgrid;public gridpop (Context context,int RESOURCEID) {super (context); this.context = Context;this.resid=resourceid;initallpop ();} public void Initallpop () {layoutinflater = (layoutinflater) context.getsystemservice (context.layout_inflater_service ); AllView = Layoutinflater.inflate (resId, null); Allview.setfocusable (true); Allview.setfocusableintouchmode (true); Allitemgrid = (GridView) Allview.findviewbyid (R.id.pop_grid); Allview.setfocusableintouchmode (true); Allview.setonkeylistener (New Onkeylistener () {@Overridepublic Boolean onKey (View v, int keycode, keyevent event) {if (IsS Howing ()) {dismiss (); return true;} return false;}});D Isplaymetrics dm=new displaymetrics ();((Activity) context). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); Setcontentview (AllView); SetWidth (DM.WIDTHPIXELS/2); SetheighT (layoutparams.wrap_content); Setbackgrounddrawable (new Colordrawable (color.transparent)); SetTouchable (true); Setfocusable (True); Setoutsidetouchable (true);} /** * * @return returns the GridView in the pop, can be set elsewhere in the GridView */public GridView Getallitemgrid () {return allitemgrid;}


Above is the code for Gridpop. Inside is the GridView, in fact pop inside can fill a lot of view. No longer repeat, after the package POPs, only need to change the layout file, change the data, in other places can be convenient new pop out of different needs. For example, in the base class activity do Popmenu Popwindow operation, then other sub-activity can be very convenient inheritance.

Look at the mainactivity.

/** * * @author Yaguang.wang * */public class Activity_main extends Activity {private Button btn1,btn2;private menupop PO Pmenu;private defaultpop defaultpop;private gridpop gridpop;private arraylist<string> stringArray;private GridView GV; @Overrideprotected void OnCreate (Bundle savedinstancestate) {Setcontentview (r.layout.main); Initview (); Super.oncreate (savedinstancestate);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {menu.add ("xx");//Trigger Onmenuopenedreturn Super.oncreateoptionsmenu (menu);} /** * Popmenu */@Overridepublic boolean onmenuopened (int featureid, menu menu) {if (popmenu!=null) {if (Popmenu.isshowing ( ) {Popmenu.dismiss ();} else {View v=this.getlayoutinflater (). Inflate (R.layout.pop_menu, null);p opmenu.showatlocation (V, gravity.bottom, 0, 0);}} False to mask the Xxreturn of menu add false;} private void Initview () {//pop Pop1 compare ugly forgive btn1 = (button) This.findviewbyid (R.ID.MAIN_BTN1);//pop Pop2 compare ugly forgive btn2 = (button) This.findviewbyid (R.ID.MAIN_BTN2); Btn1.setonclicklistener (New ClicKlistener ()); Btn2.setonclicklistener (new Clicklistener ());p Opmenu = new Menupop (R.layout.pop_menu, Activity_ main.this);//list value Stringarray = new arraylist<string> (); for (int i = 1; i <=; i++) {Stringarray.add ("item" +i );} Defaultpop = new Defaultpop (activity_main.this, r.layout.default_popwin); gridpop = new Gridpop (Activity_Main.this, r.layout.pop_list); gv = Gridpop.getallitemgrid (); Gv.setadapter (New Gridviewadapter (Activity_main.this, StringArray )); Gv.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> Parent, View view,int position, long id) {//todo do Somethingtoast.maketext (Getapplicationcontext (), ' item on clicked ' +po Sition, Toast.length_short). Show (); if (gridpop.isshowing ()) {Gridpop.dismiss ();}});} Private class Clicklistener implements view.onclicklistener{@Overridepublic void OnClick (View v) {switch (V.getid ()) {/* * * POP1 Simple control */case r.id.main_btn1:if (defaultpop!=null) {if (defaultpop.isshowing ()) {Defaultpop.disMiss ();} else {Defaultpop.showasdropdown (v);}} break;/** * POP2 Complex control */case r.id.main_btn2:if (gridpop!=null) {if (gridpop.isshowing ()) {Gridpop.dismiss ();} else {GRIDP Op.showasdropdown (v);}} Break;}}}}


Other parts of the code are not posted. Co-learning, source code, in http://download.csdn.net/detail/wangyg1990/7317203

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.