Android dialog box Use Daquan

Source: Internet
Author: User

Type of dialog box

1.AlertDialog (Dialog) System dialog box.
2.Popupwindow Pop-up dialog box.

dialog box Differences

1.AlertDialog Non-blocking dialog box: The Display of a dialog box does not affect the execution of background tasks.
2.PopupWindow Blocking dialog box: The dialog pops up and blocks the background task from executing until the dialog box disappears.

dialog Box Example dialog dialog box for class

We generally use Alertdialog dialog box, then and dialog what is the difference between, in fact Alertdialog just inherited dialog, realized some of their own methods. Therefore, Alertdialog and dialog belong to the same dialog box. Then we'll take alertdialog for a while.

Multi-button dialog box

/** * Multi-button dialog box * /    Private void Dialog1() {Alertdialog.builder Builder =NewAlertdialog.builder ( This); Builder.settitle (OK dialog box); Builder.setmessage (The test dialog box); Builder.setpositivebutton ("The best",NewDialoginterface.onclicklistener () {@Override             Public void OnClick(Dialoginterface Dialog,intwhich) {}}); Builder.setnegativebutton ("Bad",NewDialoginterface.onclicklistener () {@Override             Public void OnClick(Dialoginterface Dialog,intwhich) {}}); Builder.setneutralbutton ("Great location",NewDialoginterface.onclicklistener () {@Override             Public void OnClick(Dialoginterface Dialog,intwhich) {}});        Builder.seticon (R.drawable.ic_launcher);    Builder.show (); }
Single-selection dialog box

 /** * Single-selection dialog box * /    Private void dialog2() {String items[] = {"Item1","Item2","Item3","Item4","ITEM5"}; Alertdialog.builder Builder =NewAlertdialog.builder ( This);intPosition =0;//Default single-selection locationBuilder.settitle ("Radio dialog box"). Setsinglechoiceitems (items, position,NewDialoginterface.onclicklistener () {@Override             Public void OnClick(Dialoginterface Dialog,intwhich) {//todo to meet your needs}}). Show (); }
Multi-Select dialog box

/** * Multi-Select dialog box * /    Private void Dialog3() {//Default multi-select status        BooleanB[] = {false,false,true,false,false}; String items[] = {"Item1","Item2","Item3","Item4","ITEM5"}; Alertdialog.builder Builder =NewAlertdialog.builder ( This); Builder.settitle (Multi-Select dialog box). Setmultichoiceitems (items, B,NewDialoginterface.onmultichoiceclicklistener () {@Override             Public void OnClick(Dialoginterface Dialog,intWhich,BooleanisChecked) {//todo to meet your needs}}). Show (); }
List dialog box

/** * List dialog box * /    Private void Dialog4() {String items[] = {"Item1","Item2","Item3","Item4","ITEM5","Item6","Item7","ITEM8"}; Alertdialog.builder Builder =NewAlertdialog.builder ( This); Builder.settitle (List dialog Box). Setitems (Items,NewDialoginterface.onclicklistener () {@Override             Public void OnClick(Dialoginterface Dialog,intwhich) {//todo to meet your needs}}). Show (); }
To add a custom Layout dialog box

/** * Add Custom Layout dialog box * /    Private void Dialog5() {Alertdialog.builder Builder =NewAlertdialog.builder ( This); Builder.seticon (R.drawable.ic_launcher). Settitle (Add Layout dialog box). Setpositivebutton ("Yes",NewDialoginterface.onclicklistener () {@Override                     Public void OnClick(Dialoginterface Dialog,intwhich) {}}). Setnegativebutton ("No",NewDialoginterface.onclicklistener () {@Override             Public void OnClick(Dialoginterface Dialog,intwhich) {}}). Setmessage ("This content is based on your needs, but not"); View view = Layoutinflater.from ( This). Inflate (R.layout.items,NULL);        Alertdialog dialog = Builder.create ();        Dialog.setview (view);    Dialog.show (); }
Simple custom dialog Box 1

/**     * 简单自定义对话框1     */    privatevoiddialog6() {        new Dialog(this);        dialog.setTitle("自定义对话框1");        dialog.setContentView(R.layout.items);        //设置点击对话框内容之外对话框消失        dialog.setCanceledOnTouchOutside(true);        dialog.show();    }
Simple custom dialog Box 2

 /** * Simple custom dialog Box 2 */    Private void Dialog7() {FinalAlertdialog dialog =NewAlertdialog.builder ( This). Create (); Dialog.setcanceledontouchoutside (true);        Dialog.show ();        Dialog.getwindow (). Setcontentview (R.layout.dialog);        TextView title = (TextView) Dialog.getwindow (). Findviewbyid (R.id.title); Title.settext ("Simple custom dialog box 2"); Dialog.getwindow (). Findviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v)            {Dialog.dismiss ();    }        }); }
Advanced customization dialog box

/** * Fully customizable dialog box, including dialog box location, size, theme, animation and so on. */    Private void Dialog8() {//Instantiate dialog box and set the theme of the dialog box        FinalDialog Dialog =NewDialog ( This, R.style.selectdialog);//Set Click outside the dialog box to disappearDialog.setcanceledontouchoutside (true); Window Dialogwindow = Dialog.getwindow ();//Setting the location of the dialog boxDialogwindow.setgravity (Gravity.top);//Get dialog Box PropertiesWindowmanager.layoutparams LP = Dialogwindow.getattributes ();//Set dialog box start X axislp.x =0;//Set dialog box start Y axisLP.Y = getstatusbarheight () + getactionbarheight ();//Set dialog box sizeLp.width = WindowManager.LayoutParams.MATCH_PARENT;        Lp.height = WindowManager.LayoutParams.WRAP_CONTENT; Dialogwindow.setattributes (LP);//Set the layout of the custom dialog boxDialog.setcontentview (R.layout.dialog); Dialogwindow.findviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v)            {Dialog.dismiss ();        }        });    Dialog.show (); }/** * Get phone status bar Height * * @return  */    Private int Getstatusbarheight() {Window Dialogwindow = GetWindow (); Rect frame =NewRect (); Dialogwindow.getdecorview (). Getwindowvisibledisplayframe (frame);returnFrame.top; }/** * Get the height of actionbar * * @return  */    Private int Getactionbarheight() {returnGetsupportactionbar (). GetHeight (); }
Popupwindow Type of dialog box

As the name implies, pop-up dialog box, although the Alertdialog dialog box is basically enough to meet the daily development of the hanging wire programmer, but the Alertdialog dialog box is not flexible enough, so there is a fully customized, flexible Popupwindow pop-up dialog box.

code example

Private void Dialog9() {View view = Layoutinflater. from( This). Inflate (R.layout.dialog,NULL);//Instantiate the PW dialog box and set the layout, size, whether the focus is available, the third parameter is true to get the focus, the general setting is trueFinal Popupwindow pw =NewPopupwindow (view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,true);//If the dialog box has the focus, this is true when clicking outside of PW does not respond, otherwise click Response. Pw.setfocusable (true);//Originally clicked the window outside of PW disappears, but the experiment found invalid, only when the setbackgrounddrawable is set to be valid. //Pw.setoutsidetouchable (TRUE);        //Set the animation effect of the PW dialog boxPw.setanimationstyle (R.style.animation1);//This is very important, set the PW dialog box background is fully transparent, only set this, click the PW dialog outside the content, the dialog box disappears, and the dialog box can respond back to return the key. Pw.setbackgrounddrawable (NewColordrawable (0x00000000));//Set the position offset of the dialog box        intx =0;inty = getstatusbarheight () + getactionbarheight ();//Display dialog box relative to parent controlPw.showatlocation (Parentview, Gravity.top, x, y);//Showasdropdown (View anchor): relative to the position of a control (just below the left), no offset//Showasdropdown (View anchor, int xoff, int yoff): Offset relative to the position of a control//Showatlocation (View parent, int gravity, int x, int y): relative to the position of the parent control (for example, central Gravity.center, Lower gravity.bottom, etc.), you can set Offset or no offset        //pw the dialog box to set the translucent background. Principle: When PW displays, the transparency of the entire window is changed to 0.7, and when PW disappears, the transparency is 1Final Windowmanager.layoutparamsparams= Mainactivity. This. GetWindow (). GetAttributes ();params. Alpha =0.7F Mainactivity. This. GetWindow (). SetAttributes (params); View.findviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View v) {Isexit =true; Pw.dismiss ();params. Alpha =1F Mainactivity. This. GetWindow (). SetAttributes (params); }        });//PW dialog box disappears listener eventPw.setondismisslistener (NewPopupwindow.ondismisslistener () {@Override Public void Ondismiss() {params. Alpha =1F Mainactivity. This. GetWindow (). SetAttributes (params);    }        }); }
Topics used in the above code
<stylename="Selectdialog"> <Item name="Android:windownotitle">true</Item> <Item name="Android:windowistranslucent">false</Item> <Item name="Android:windowbackground"> @color/white</Item> <Item name="Android:windowanimationstyle"> @style/animation1</Item> </style> <stylename="Animation1"Parent="Android:animation"> <Item name="Android:windowenteranimation"> @anim/top2down</Item> <Item name="Android:windowexitanimation"> @anim/bottom2top</Item> </style>
Popupwindow Use note points:

1.PopupWindow Focus Issues:
Setfocusable (Boolean B); True when the dialog box is not clickable, conversely, the system is set to True by default, to prevent accidental, general setfocusable (true).

2.PopupWindow does not respond to back return key issues:
To solve this problem, simply set the dialog box background transparent pw.setbackgrounddrawable (0x00000000), and then click outside the dialog box after setting up the PW dialog is also closed. It is worth noting that setting pw.setoutsidetouchable (false) is not valid.

3.PopupWindow Background issues:
The method I use here is: when the pw dialog pops up, it changes the transparency Alpha property of the current window window and changes the transparency of the current Windows back when the dialog box is closed. Refer to the above code for details.

The android dialog box uses a summary:

Daily development is nothing more than the above dialog boxes, as for the implementation of different effects can be modified according to requirements, but you can find the prototype from the above code.

SOURCE Link http://download.csdn.net/detail/feidu804677682/8715625

Keep on learning, keep summarizing

Android dialog box Use Daquan

Related Article

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.