When we do development in peacetime, inevitably will use a variety of dialog boxes, I believe that have other platform development experience of friends will know that most of the platform is only to provide a few of the simplest implementation, assuming 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 also 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 a dialog box for such a situation?
Fortunately, Android provides a solution to this problem, and when I first approached Android, I was doing a self-defining dialog, and it was done in a way that was inherited, and later, with a deeper understanding of the document, Found that the Android start has provided the corresponding interface dialog Builder, the following I would like to share the relevant content here, but also let many others just start learning less detours.
The first is a simple application that pops up a message box that can be implemented in Android
1new Alertdialog.builder (self) 2. Settitle ("title") 3. Setmessage ("Simple Message box") 4 . Setpositivebutton ("OK", NULL) 5. Show ();
Effects such as the following:
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 of the parameters of Self, This is actually a reference to the activity object, and it is possible to pass in the corresponding reference according to the context in which you are located. For example, in the OnCreate method called, only need to pass this on.
The following is a dialog box with the Confirm and Cancel button
- 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, in fact, this is 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.
The following is a dialog box that allows you to enter text
- 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 the above code, we use the Setview method, for our dialog box passed a text edit box, of course, you can pass whatever the view object, such as picture frame, webview, etc... Play with your imagination, ~:lol.
The following is a single box and a multi-box, but also a useful two kinds of dialog boxes
- 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 Dialogint Erface. Onclicklistener () {public void OnClick (Dialoginterface dialog, int which) {dialog . dismiss (); }}). Setnegativebutton ("Cancel", null). Show ();
-
- 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 good understanding, the following is the final introduction of two,
List dialog box
- 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
- 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, detailed implementation method does not introduce, leave everyone to practice it ~:lol
Finally, the Android platform for our development to provide a great convenience, Dialogbuilder can do more than this, here to show you just the tip of the iceberg, we can enjoy the imagination, create our own dialog box.
Frequently used Android Pop-up dialog box