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 ) |
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) |
|
. Setmessage ( "Are you sure you want? " ) |
|
. Setpositivebutton ( "Yes" , Null ) |
|
. Setnegativebutton ( "No" , Null ) |
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) |
|
. Seticon (Android. R. drawable. ic_dialog_info) |
|
. Setview ( New Edittext (Self )) |
|
. Setpositivebutton ( "OK" , Null ) |
|
. Setnegativebutton ( "Cancel" , Null ) |
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) |
|
. 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 ){ |
|
. Setnegativebutton ( "Cancel" , Null ) |
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 ) |
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 ) |
Finally, the image is displayed in the dialog box:
1 |
Imageview IMG = New Imageview (Self ); |
2 |
IMG. setimageresource (R. drawable. Icon ); |
4 |
New Alertdialog. Builder (Self) |
5 |
. Settitle ( "Image box" ) |
7 |
. Setpositivebutton ( "OK" , Null ) |
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 ) |
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) |
|
. Setmessage ( "Are you sure you want? " ) |
|
. Setpositivebutton ( "Yes" , Null ) |
|
. Setnegativebutton ( "No" , Null ) |
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) |
|
. Seticon (Android. R. drawable. ic_dialog_info) |
|
. Setview ( New Edittext (Self )) |
|
. Setpositivebutton ( "OK" , Null ) |
|
. Setnegativebutton ( "Cancel" , Null ) |
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) |
|
. 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 ){ |
|
. Setnegativebutton ( "Cancel" , Null ) |
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 ) |
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 ) |
Finally, the image is displayed in the dialog box:
1 |
Imageview IMG = New Imageview (Self ); |
2 |
IMG. setimageresource (R. drawable. Icon ); |
4 |
New Alertdialog. Builder (Self) |
5 |
. Settitle ( "Image box" ) |
7 |
. Setpositivebutton ( "OK" , Null ) |
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.