Android development process, often encounter a number of demand scenarios-pop a box on the interface, to remind users and let users do some selective operation,
If you exit the pop-up window, let the user choose "Quit" or "Cancel" actions.
The Android System provides dialog classes, as well as subclasses of dialog, common as alertdialog to implement such functions.
In general, the dialog and its subclasses provided by Android can meet most of these needs, however, its shortcomings are reflected in:
1. Android-based dialog and its sub-class style is a single, style and the app itself may not be in harmony with the style;
2. The dialog window is limited in layout and functionality, and sometimes does not necessarily meet the actual business requirements.
This article will build a custom dialog pop-up window based on dialog, taking the most common confirmation box as an example.
This style is relatively simple: there is a bullet box title (prompt), the following is the "Confirm" and "Cancel" button, when the user clicks the "Confirm" button, the box execution
The corresponding confirmation logic, when the "Cancel" button is clicked, the corresponding cancellation logic is executed.
First, customize the frame style:
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "3 android:layout_width=" match_parent "4 android:layout_height=" wrap_content "5 android:background=" @d RAWABLE/DIALOG_BG "6 android:orientation=" vertical "> 7 8 <textview 9 android:id=" @+id/title "10 Android:layout_width= "Wrap_content" one android:layout_height= "Wrap_content" android:layout_gravity= " Center "android:paddingtop=" 14DP "android:textcolor=" @color/login_hint "android:textsize=" @d Imen/text_size_18 "/>16 match_parent <linearlayout18 android:layout_width=" Android:layout_ " height= "Wrap_content" android:layout_marginbottom= "14DP" android:layout_marginleft= "20DP" droid:layout_marginright= "20DP" android:layout_margintop= "30DP" >24 <textview26 and Roid:id= "@+id/confirm" 27 Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layou t_marginright= "10DP" android:layout_weight= "1" android:background= "@drawable/btn_confirm_selec Tor "android:gravity=" center "android:textcolor=" @color/white "android:textsize= "@dimen/text_size_16"/>35 <textview37 android:id= "@+id/cancel" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "10DP" 41 android:layout_weight= "1" android:background= "@drawable/btn_cancel_selector" Androi d:gravity= "center" android:textcolor= "@color/login_hint" android:textsize= "@dimen/text_size_16 "/>46 </linearlayout>47 </LinearLayout>
Then, build the confirmation box control Confirmdialog by inheriting the dialog class:
1 package com.corn.widget; 2 3 Import Android.app.Dialog; 4 Import Android.content.Context; 5 Import Android.os.Bundle; 6 Import Android.util.DisplayMetrics; 7 Import Android.view.LayoutInflater; 8 Import Android.view.View; 9 Import android.view.window;10 Import android.view.windowmanager;11 import android.widget.textview;12 Import COM.CORN.R;14 public class Confirmdialog extends Dialog {+ + private Context context;18 private String title; private string confirmbuttontext;20 private string cacelbuttontext;21 private Clicklistenerinterface Clickl ISTENERINTERFACE;22-Public interface Clicklistenerinterface {The public void doconfirm (); Blic void Docancel ();}29 public Confirmdialog (context context, string title, String Confirmbuttontext, Strin G Cacelbuttontext) {To super (context, r.style.mydialog); this.context = context;33 This.title = title;34 This.confirmbuttontext = Confirmbuttontext;35 this.cacelbuttontext = cacelbuttontext;36}37 @Override39 protected void OnCreate (Bundl E savedinstancestate) {//TODO auto-generated method stub41 super.oncreate (savedinstancestate); 42 43 Init ();}45-public void init () {Layoutinflater Inflater = layoutinflater.from (context); 48 View view = Inflater.inflate (R.layout.confirm_dialog, null), Setcontentview (view), TextView t Vtitle = (TextView) View.findviewbyid (r.id.title); TextView tvconfirm = (TextView) View.findviewbyid (R.id.confir m); TextView Tvcancel = (TextView) View.findviewbyid (r.id.cancel); Tvtitle.settext (title); 56 Tvconfirm.settext (Confirmbuttontext); tvconfirm.setonclicklist Tvcancel.settext (Cacelbuttontext); Ener (New Clicklistener ()); Tvcancel.setonclicklistener (new Clicklistener ()); GetWindow ();Indowmanager.layoutparams LP = Dialogwindow.getattributes (); Displaymetrics d = context.getresources (). GetDispla Ymetrics (); Get screen width, height with lp.width = (int) (D.widthpixels * 0.8); The height is set to the screen of 0.666 Dialogwindow.setattributes (LP);}68 public void Setclicklistener (Clicklistenerinterf Ace Clicklistenerinterface) {this.clicklistenerinterface = clicklistenerinterface;71}72 private CLA SS Clicklistener implements View.onclicklistener {@Override75 public void OnClick (View v) {76 TODO auto-generated method stub77 int id = v.getid (); switch (ID) {$ case R.I D.confirm:80 clicklistenerinterface.doconfirm (); Bayi break;82 Case R.id.cancel:8 3 Clicklistenerinterface.docancel (); break;85}86}87 88};89 90 }
In the above space construction code, because the control's "ACK" and "Cancel" logic is related to the actual scenario, the control is implemented by defining an internal interface.
Where this control needs to be used, the following form of invocation is made:
1 public static void Exit (final context context) {2 final confirmdialog confirmdialog = new Confirmdialog (Context, "correct Do you want to exit? "," Exit "," Cancel "); 3 confirmdialog.show (); 4 Confirmdialog.setclicklistener (new Confirmdialog.clicklistenerinterface () {5 @Override 6 public void Doconfirm () {7 //TODO auto-generated method stub 8 Confirmdialog.dismiss (); 9< C7/>//touserhome (context); Appmanager.getappmanager (). AppExit (context), }12 @Override14 public void Docancel () { //TODO auto-generated method Stub16 Confirmdialog.dismiss (); }18 });
The internal interface of this control is implemented in the call and is assigned to the control itself to implement a function callback based on the external specific business logic when the button is clicked.
Android Custom Dialog