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
New Alertdialog.builder (self). Settitle ("title")
. Setmessage (
"简单消息框"
)
|
.setPositiveButton( "确定" , null ) |
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:
|
new AlertDialog.Builder(self) |
|
.setPositiveButton( "是" , null ) |
|
.setNegativeButton( "否" , null ) |
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:
|
new AlertDialog.Builder(self) |
|
.setIcon(android.R.drawable.ic_dialog_info) |
|
.setView( new EditText(self)) |
|
.setPositiveButton( "确定" , null ) |
|
.setNegativeButton( "取消" , null ) |
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. Make the most of your imagination. Here are the radio boxes and the multi-box, which are also useful for both dialog boxes:
|
new AlertDialog.Builder(self) |
|
.setIcon(android.R.drawable.ic_dialog_info) |
|
.setSingleChoiceItems( new String[] { "选项1" , "选项2" , "选项3" , "选项4" }, 0 , |
|
new DialogInterface.OnClickListener() { |
|
public void onClick(DialogInterface dialog, int which) { |
|
.setNegativeButton( "取消" , null ) |
1 |
new AlertDialog.Builder(self) |
3 |
.setMultiChoiceItems( new String[] { "选项1" , "选项2" , "选项3" , "选项4" }, null , null ) |
4 |
.setPositiveButton( "确定" , null ) |
5 |
.setNegativeButton( "取消" , null ) |
Radio and multi-select dialogs should be a lot of our usual use, the code should be very well understood, the following is the final introduction of two List dialog box:
1 |
new AlertDialog.Builder(self) |
3 |
.setItems( new String[] { "列表项1" , "列表项2" , "列表项3" }, null ) |
4 |
.setNegativeButton( "确定" , null ) |
Finally, the picture is displayed in the dialog box:
1 |
ImageView img = new ImageView(self); |
2 |
img.setImageResource(R.drawable.icon); |
4 |
new AlertDialog.Builder(self) |
7 |
.setPositiveButton( "确定" , null ) |
We passed a imageview to show 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 ~ finally summed up, 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.
"Go" Android detailed dialog box Alertdialog.builder use method