When we do development in peacetime, inevitably will use a variety of dialog boxes, I believe that there are other platform development experience of friends will know that most of the platform is only to provide a few of the simplest implementation, if we want to achieve their own specific needs of the dialog box, you may first think, through inheritance and other ways, Rewrite our own dialog box. Of course, this is a good solution, but the general situation is that, we rewrite the dialog box, perhaps only in a specific place will be used, in order to use this time, and to create a new class, often a little overkill feeling, and even add unnecessary complexity to our program, Is there a more elegant solution to the dialog box for this situation?
Fortunately, Android offers a solution to this problem, and when I first approached Android, I was doing a custom dialog, and it was done in a way that was inherited, and later, with a deeper understanding of the document, it was discovered that the Android starter already provided the appropriate interface dialog Builder, below I will be related to the content here to share, but also to make more beginners less detours.
1. The simplest message popup box
New Alertdialog.builder (self). settitle ("title") . Setmessage ("Simple message box") . Setpositivebutton (null) . Show ();
:
In the above code we create a new alertdialog, and use the Builder method to form an object chain, through a series of setup methods, construct the dialog box we need, and then call the Show method to display, notice the builder method parameter self, This is actually a reference to the activity object, and it is possible to pass in the appropriate reference according to the context in which you are located. For example, called in the OnCreate method, just pass in this.
2. dialog box with confirmation and Cancel button
New Alertdialog.builder (self). Settitle ("confirm"). Setmessage ("OK?"). "). Setpositivebutton (null). Setnegativebutton (null). Show ();
:
Notice that there are two null parameters, here is actually the two button click on the Listener, because we do not need to listen to these actions, so the incoming null value is simply ignored, but the actual development of the time is generally required to pass the listener, in response to the user's operation.
3. dialog box that can enter text
New Alertdialog.builder (self). Settitle ("Please enter"). SetIcon (Android. R.drawable.ic_dialog_info). Setview (new EditText (self)). Setpositivebutton (null ). Setnegativebutton (null). Show ();
As on the code, we use the Setview method, for our dialog box passed a text edit box, of course, you can pass any view object, than the frame, webview and so on. Play with your imagination, ~:lol.
4. Two dialog boxes for Radio box and multi box
New Alertdialog.builder (self). Settitle ("Please select"). SetIcon (Android. R.drawable.ic_dialog_info) . Setsinglechoiceitems (new string[] {"Option 1", "Option 2", "option 3", "Option 4"}, 0, New Dialoginterface.onclicklistener () { publicvoidint which) { Dialog.dismiss (); } }). Setnegativebutton (null). Show ();
New Alertdialog.builder (self). Settitle ("multi Box"). Setmultichoiceitems (newnull Null). Setpositivebutton (null). Setnegativebutton (null). Show ();
New Alertdialog.builder (self). Settitle ("multi Box"). Setmultichoiceitems (newnull Null). Setpositivebutton (null). Setnegativebutton (null). Show ();
5. List dialog box
New Alertdialog.builder (self). Settitle ("list box"). Setitems (newnull). Setnegativebutton (null). Show ();
6. Display the picture in the dialog box
New ImageView (self); Img.setimageresource (R.drawable.icon); New Alertdialog.builder (self). Settitle ("picture box"). Setview (IMG). Setpositivebutton (null). Show ();
We passed a imageview to display the picture, here shows a classic Android small green man icon ~ ~, of course, here can also put on the network picture, the specific implementation method is not introduced, leave everyone to practice it ~:lol
Finally, the Android platform for our development to provide a great convenience, Dialogbuilder can do more than these, here to show you just the tip of the iceberg, we can enjoy the imagination, create our own dialog box.
Android Learning-common Android Pop-up dialog box