"Go" Android detailed dialog box Alertdialog.builder use method

Source: Internet
Author: User

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)
.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:

newAlertDialog.Builder(self) 
.setTitle("确认")
.setMessage("确定吗?")
.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. Here is a dialog box that allows you to enter text:

newAlertDialog.Builder(self)
.setTitle("请输入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(newEditText(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. Make the most of your imagination. Here are the radio boxes and the multi-box, which are also useful for both dialog boxes:
newAlertDialog.Builder(self)
.setTitle("请选择")
.setIcon(android.R.drawable.ic_dialog_info)                
.setSingleChoiceItems(newString[] {"选项1","选项2","选项3","选项4"},0
  newDialogInterface.OnClickListener() {
                             
     publicvoid onClick(DialogInterface dialog, intwhich) {
        dialog.dismiss();
     }
  }
)
.setNegativeButton("取消",null)
.show();
1 newAlertDialog.Builder(self)
2 .setTitle("多选框")
3 .setMultiChoiceItems(newString[] {"选项1","选项2","选项3","选项4"}, null,null)
4 .setPositiveButton("确定",null)                
5 .setNegativeButton("取消",null)
6 .show();
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 newAlertDialog.Builder(self)
2 .setTitle("列表框")
3 .setItems(newString[] {"列表项1","列表项2","列表项3"},null)
4 .setNegativeButton("确定",null)
5 .show();
Finally, the picture is displayed in the dialog box:
1 ImageView img =newImageView(self);
2 img.setImageResource(R.drawable.icon);
3  
4 newAlertDialog.Builder(self)
5 .setTitle("图片框")
6 .setView(img)
7 .setPositiveButton("确定",null)
8 .show();
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.