In Android development, you often encounter the need to play window reminders. And the system comes with pop-up dialog box is not very beautiful, you can inherit the dialog box Class (Dialog), and set the custom layout file, to beautify the pop-up dialog box purposes.
The first is to customize a dialog class, inherit from the dialog, and then in the constructor in the dialog box to display Content, button response function, and other parameters:
The code is as follows |
Copy Code |
Public Userdefineddialog (context, String, View.onclicklistener Onclicklistener, View.onclicklistener Cancellistener) { super ( Context,r.style.theme_dialog); THIS.CTX = context; THIS.MSG = message; if (onclicklistener!= null) { this.oklistener=onclicklistener; } if (cancellistener!= null) { istwobutton=true; This.cancellistener=cancellistener; } } |
Then, in the OnCreate function of the custom dialog class, set the layout of the dialog box and the display style:
The code is as follows |
Copy Code |
protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.alertdialog);
Tvtitle = (TextView) Findviewbyid (r.id.dialogtitle); Tvcontent = (TextView) Findviewbyid (r.id.dialogcontent);
Btnleft = (Button) Findviewbyid (r.id.btnleft); Btnright = (Button) Findviewbyid (r.id.btnright);
Btnleft.setonclicklistener (this); Btnright.setonclicklistener (this);
Btncenter = (Button) Findviewbyid (r.id.btncenter); Btncenter.setonclicklistener (this); if (Istwobutton) { Btnleft.setvisibility (view.visible); Btnright.setvisibility (view.visible); Btncenter.setvisibility (View.gone); } Tvtitle.settext ("hint"); Tvcontent.settext (msg);
WindowManager m; m = (WindowManager) ctx.getsystemservice (Context.window_service); Display d = m.getdefaultdisplay (); Layoutparams p = GetWindow (). GetAttributes (); Set dialog box width height P.height = (int) (D.getheight () * 0.3); P.width = (int) (D.getwidth () * 0.9); Set dialog box Transparency P.alpha = 0.8f; Let the background darken P.dimamount = 0.7f; GetWindow (). SetAttributes (P); GetWindow (). setgravity (Gravity.center); } |
In addition, the custom dialog class Userdefineddialog implements the Onclicklistener interface to implement the response to the button on the click Dialog, and in the response function, determines whether the corresponding response function is passed in when the class is constructed and processed by the function:
The code is as follows |
Copy Code |
public void OnClick (View v) { Switch (V.getid ()) { Case R.id.btnleft: if (Cancellistener!= null) { Cancellistener.onclick (v); } Break Case R.id.btncenter: if (Oklistener!= null) { Oklistener.onclick (v); } Break Case R.id.btnright: if (Oklistener!= null) { Oklistener.onclick (v); } Break } This.cancel (); } |
Finally, the call is very simple:
The code is as follows |
Copy Code |
New Userdefineddialog (This, "I am a dialog box with a button, the default effect is canceled", NULL, NULL). Show ();
New Userdefineddialog (This, "I'm a dialog box with two buttons", New Onclicklistener () { public void OnClick (View v) { DoSomething } }, New Onclicklistener () { public void OnClick (View v) { DoSomething} ). Show (); |
After the beautification of the dialog box as shown in the following figure:
custom dialog box-single button