Android detailed dialog box alertdialog. Builder usage

Source: Internet
Author: User
We usually do Development You can use a variety of dialog boxes. Platform Developers will know that most platforms only provide a few simple implementations. If we want to implement a dialog box that meets our specific needs, you may first think of it, rewrite our own dialog box through inheritance and other methods. Of course, this is also a good Solution Method, but in general, this is the case. We may only use the rewrite dialog box in a specific place. To use it this time, we will create a new class, it's often a bit cool-killing, even for us. Program Add unnecessary complexity. Is there a more elegant solution to the dialog box in this case?
Fortunately, Android I provided a solution to this problem. When I first started to access Android, I was working on a self-built Definition The dialog box is also implemented through inheritance. Document After in-depth understanding, I found that the corresponding interface dialog builder has been provided at the beginning of Android. Here I will share the relevant content, so that more beginners can avoid detours.

The first is the simplestApplicationIs to pop up a message box, which can be implemented in Android

New alertdialog. Builder (Self)
. Settitle ("title ")
 
. Setmessage ("Simple message box")

  . Setpositivebutton ("OK", Null)
  . Show ();

 

The effect is as follows:

 

 

The aboveCodeWe created an alertdialog and formed an object chain using the builder method. Through a series of setting methods, we constructed the required dialog box.

After the show method is called, it is displayed, and the builder method parameter self is noticed. This is actually a reference to the activity object. You can input the corresponding reference according to your context.
Yes. For example, to call the oncreate method, you only need to input this.

 

The following is a dialog box with the confirm and cancel buttons:

 

  New Alertdialog. Builder (Self)
  . Settitle ("Confirm")
  . Setmessage ("Are you sure you want? ")
  . Setpositivebutton ("Yes", Null)
  . Setnegativebutton ("No", Null)
  . Show ();

 

 

 

 

 

Note that there are two null parameters, which are actually listening programs for clicking these two buttons. because we do not need to listen for these actions, the null value is ignored, however, in actual development, listeners must be input to respond to user operations.
The following is a dialog box that allows you to enter text:

 

  New Alertdialog. Builder (Self)
  . Settitle ("Enter")
  . Seticon (Android. R. drawable. ic_dialog_info)
  . Setview (New Edittext (Self ))
  . Setpositivebutton ("OK", Null)
  . Setnegativebutton ("Cancel", Null)
  . Show ();

 

In the above Code, we use the setview method to input a text edit box for our dialog box. Of course, you can input any view object, such as the slice box and webview .. Make full use of your imagination ~ The following are single-choice and multi-choice boxes, which are two useful dialog boxes:
  New Alertdialog. Builder (Self)
  . Settitle ("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 ();
1 New Alertdialog. Builder (Self)
2 . Settitle ("Multi-choice box")
3 . Setmultichoiceitems (New String [] {"Option 1","Option 2","Option 3","Option 4"}, Null, Null)
4 . Setpositivebutton ("OK", Null)
5 . Setnegativebutton ("Cancel", Null)
6 . Show ();
The single-choice dialog box and multi-choice dialog box should be frequently used, and the code should be well understood. Next we will introduce the two
List dialog box:
1 New Alertdialog. Builder (Self)
2 . Settitle ("List box")
3 . Setitems (New String [] {"List item 1","List item 2","List item 3"}, Null)
4 . Setnegativebutton ("OK", Null)
5 . Show ();
Finally, the image is displayed in the dialog box:
1 Imageview IMG = New Imageview (Self );
2 IMG. setimageresource (R. drawable. Icon );
3   
4 New Alertdialog. Builder (Self)
5 . Settitle ("Image box")
6 . Setview (IMG)
7 . Setpositivebutton ("OK", Null)
8 . Show ();

We passed in an imageview to display the image. Here we show a classic Android little green person icon ~ ~, Of course, you can also put network images here. The specific implementation method will not be introduced. Let's take a look at it ~
Finally, the Android platform provides great convenience for our development. What dialogbuilder can do is just the tip of the iceberg. We can give full play to our imagination, create our own dialog box. We usually doDevelopmentYou can use a variety of dialog boxes.PlatformDevelopers will know that most platforms only provide a few simple implementations. If we want to implement a dialog box that meets our specific needs, you may first think of it, rewrite our own dialog box through inheritance and other methods. Of course, this is also a goodSolutionMethod, but in general, this is the case. We may only use the rewrite dialog box in a specific place. To use it this time, we will create a new class, it's often a bit cool-killing, even for us.ProgramAdd unnecessary complexity. Is there a more elegant solution to the dialog box in this case?
Fortunately,AndroidI provided a solution to this problem. When I first started to access Android, I was working on a self-builtDefinitionThe dialog box is also implemented through inheritance.DocumentAfter in-depth understanding, I found that the corresponding interface dialog builder has been provided at the beginning of Android. Here I will share the relevant content, so that more beginners can avoid detours.

The first is the simplestApplicationIs to pop up a message box, which can be implemented in Android

New alertdialog. Builder (Self)
. Settitle ("title ")
 
. Setmessage ("Simple message box")

  . Setpositivebutton ("OK", Null)
  . Show ();

 

The effect is as follows:

 

 

In the code above, we created an alertdialog and formed an object chain using the builder method. Through a series of setting methods, we constructed the required dialog box.

After the show method is called, it is displayed, and the builder method parameter self is noticed. This is actually a reference to the activity object. You can input the corresponding reference according to your context.
Yes. For example, to call the oncreate method, you only need to input this.

 

The following is a dialog box with the confirm and cancel buttons:

 

  New Alertdialog. Builder (Self)
  . Settitle ("Confirm")
  . Setmessage ("Are you sure you want? ")
  . Setpositivebutton ("Yes", Null)
  . Setnegativebutton ("No", Null)
  . Show ();

 

 

 

 

 

Note that there are two null parameters, which are actually listening programs for clicking these two buttons. because we do not need to listen for these actions, the null value is ignored, however, in actual development, listeners must be input to respond to user operations.
The following is a dialog box that allows you to enter text:

 

  New Alertdialog. Builder (Self)
  . Settitle ("Enter")
  . Seticon (Android. R. drawable. ic_dialog_info)
  . Setview (New Edittext (Self ))
  . Setpositivebutton ("OK", Null)
  . Setnegativebutton ("Cancel", Null)
  . Show ();

 

In the above Code, we use the setview method to input a text edit box for our dialog box. Of course, you can input any view object, such as the slice box and webview .. Make full use of your imagination ~ The following are single-choice and multi-choice boxes, which are two useful dialog boxes:
  New Alertdialog. Builder (Self)
  . Settitle ("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 ();
1 New Alertdialog. Builder (Self)
2 . Settitle ("Multi-choice box")
3 . Setmultichoiceitems (New String [] {"Option 1","Option 2","Option 3","Option 4"}, Null, Null)
4 . Setpositivebutton ("OK", Null)
5 . Setnegativebutton ("Cancel", Null)
6 . Show ();
The single-choice dialog box and multi-choice dialog box should be frequently used, and the code should be well understood. Next we will introduce the two
List dialog box:
1 New Alertdialog. Builder (Self)
2 . Settitle ("List box")
3 . Setitems (New String [] {"List item 1","List item 2","List item 3"}, Null)
4 . Setnegativebutton ("OK", Null)
5 . Show ();
Finally, the image is displayed in the dialog box:
1 Imageview IMG = New Imageview (Self );
2 IMG. setimageresource (R. drawable. Icon );
3   
4 New Alertdialog. Builder (Self)
5 . Settitle ("Image box")
6 . Setview (IMG)
7 . Setpositivebutton ("OK", Null)
8 . Show ();

We passed in an imageview to display the image. Here we show a classic Android little green person icon ~ ~, Of course, you can also put network images here. The specific implementation method will not be introduced. Let's take a look at it ~
Finally, the Android platform provides great convenience for our development. What dialogbuilder can do is just the tip of the iceberg. We can give full play to our imagination, create our own dialog box.

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.