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.
The first is a simple application that pops up a message box that can be implemented in Android
[Java]View Plaincopy
[Java]View Plaincopy
- 1
- New Alertdialog.builder (self)
- 2
- . Settitle ("title")
- 3
- . Setmessage ("Simple message box")
- 4
- . Setpositivebutton ("OK", null)
- 5
- . Show ();
The effect is as follows:
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.
The following is a dialog box with the Confirm and Cancel buttons
- [Java] view plaincopy
- New Alertdialog.builder (self)
- . Settitle ("confirm")
- . Setmessage ("OK? ")
- . Setpositivebutton ("yes", null)
- . Setnegativebutton ("No", null)
- . Show ();
Copy Code
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.
Here is a dialog box that allows you to enter text
- [Java] view plaincopy
- New Alertdialog.builder (self)
- . Settitle ("Please enter")
- . SetIcon (Android. R.drawable.ic_dialog_info)
- . Setview (new EditText (self))
- . Setpositivebutton ("OK", null)
- . Setnegativebutton ("Cancel", 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.
Here are the radio boxes and the multi-box, which are also useful for both dialog boxes
- [Java]View Plaincopy
- 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 () {
- public void OnClick (Dialoginterface dialog, int which) {
- Dialog.dismiss ();
- }
- }
- )
- . Setnegativebutton ("Cancel", null)
- . Show ();
- [Java]View Plaincopy
- New Alertdialog.builder (self)
- . Settitle ("multiple marquee")
- . Setmultichoiceitems (new string[] {"option 1","option 2","option 3","Option 4"}, null, null)
- . Setpositivebutton ("OK", null)
- . Setnegativebutton ("Cancel", null)
- . Show ();
Radio and Multi-select dialog should be we usually use a lot of, the code should be very well understood, the following is the final introduction of two,
List dialog box
- [Java] view plaincopy
- New Alertdialog.builder (self)
- . Settitle ("list box")
- . Setitems (new string[] {"list item 1","list item 2","list item 3"}, null)
- . Setnegativebutton ("OK", null)
- . Show ();
Finally, the picture is displayed in the dialog box
- [Java] view plaincopy
- ImageView img = new ImageView (self);
- Img.setimageresource (R.drawable.icon);
- New Alertdialog.builder (self)
- . Settitle ("Picture frame")
- . Setview (IMG)
- . Setpositivebutton ("OK", 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.
Popular Android Pop-up dialog box