First look at the various styles of dialog boxes:
We see that dialog has a lot of subclasses implemented, so we're going to define a dialog box that uses its subclasses to instantiate one, instead of just using the dialog parent class to construct it.
Second, Alertdialog
What we are focusing on today is the Alertdialog dialog, and we see that Alertdialog is a direct subclass of dialog.
Using Alertdialog, we can display a caption, up to 3 button actions, and a set of selection boxes or a popup box of your own definition.
Here's an image from the official Android document that provides a look at the composition of the Alertdialog box:
① Area 1 There is the header information that defines the popup, including the title or an icon.
② Area 2 There is the Content section of the Alertdialog dialog box, where we can set some message information, or define a set of selection boxes, and define our own layout popup.
③ Area 3 There makes our Action buttons section, where we can define our action buttons.
When it comes to action buttons here's a special note:
In Alertdialog, the definition of buttons is done through the Setxxxbutton method, with a total of 3 different action buttons for us to choose from:
1.setPositiveButton (charsequence text, Dialoginterface.onclicklistener listener) This is a button equivalent to OK, OK operation, 2. Setnegativebutton (charsequence text, Dialoginterface.onclicklistener listener) This is a button that is equivalent to canceling the operation. 3. Setneutralbutton (charsequence text, Dialoginterface.onclicklistener listener) This is the equivalent of a ignore action button.
Each of our action buttons can appear at most one, that is, the pop-up dialog can only have one positivebutton.
Let's take a look at a few of the Alertdialog dialog boxes we use in a specific instance.
1. Pop up a warning box and have three buttons to select
Let's take a look at the Code section:
Button.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//by Alertdialog.builder This class to instantiate one of our Alertdialog objects Alertdialog.builder Builder = New Alertdialog.builder (Mainactivity.this); Set the icon for title Builder.seticon (r.drawable.ic_launcher); Sets the content of the title Builder.settitle ("Pop-up Warning box"); Set content to display a message builder.setmessage ("OK to delete?"). "); Set a Positivebutton Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (Mainactivity.this, "positive:" + which, Toast.length_short). Show (); } }); Set up a Negativebutton Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.mak EText (Mainactivity.this, "negative:" + which, Toast.length_short). Show (); } }); Set a Neutralbutton Builder.setneutralbutton ("Ignore", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (Mainactivity.this, "neutral:" + which, Toast.length_short). Show (); } }); Displays the dialog box Builder.show (); } });
If we want to create a alertdialog dialog box, we need to use an inner class of Alertdialog, That is, Alertdialog.builder to build a alertdialog dialog box, and then use the Setxx method to set what we want to display.
As we can see, we've set up 3 action buttons, each of which binds a dialoginterface.onclicklistener () listener event, and then inside the Toast Toast dialog box ( This is explained in a later essay) to pop up some of our information, the which method represents the int value represented by the action button:
Positive: -1negative: -2neutral: -3
We can know, Which=-1 is the click is the OK button, 2 means the click is the Cancel button, 3 means that the click is the Ignore button.
2. Drop-down list pop-up box
The key code is as follows:
Button2.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View arg0) { Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.seticon (r.drawable.ic_launcher); Builder.settitle ("Select a City"); Specify the display data for the drop-down list final string[] cities = {"Guangzhou", "Shanghai", "Beijing", "Hong Kong", "Macau"}; set a drop-down list selection builder.setitems (cities, New Dialoginterface.onclicklistener () { @Override Public void OnClick (dialoginterface dialog, int which) { Toast.maketext (mainactivity.this, "Selected city:" + Cities[which], toast.length_short). Show (); } ); Builder.show (); } });
Here we set one of our drop-down list boxes through the Setitems (charsequence[] items, Dialoginterface.onclicklistener listener) method. Note: Because the drop-down list box or the Lado box is displayed in the content, the message and drop-down list boxes are not available at the same time .
We can also bind it to a Dialoginterface.onclicklistener listener, and when an option is selected, the dialog box disappears . The which here represents the index of each option in the drop-down list, which we can easily get the user to select which option.
3. Pop-up a drop-down radio box
Button3.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.seticon (R.drawable.ic_launcher); Builder.settitle ("Please select Gender"); Final string[] sex = {"Male", "female", "Unknown gender"}; Set a single-selection drop-down box/** * The first parameter specifies the data collection of a set of drop-down boxes that we want to display * The second parameter represents the index, specifying which radio box is selected by default On, 1 means the default ' female ' will be ticked on * The third argument to each single option binding a listener */builder.setsinglechoiceitems (sex, 1, new Dialoginterface.onclicklistener () {@Override public void OnC Lick (dialoginterface dialog, int which) {Toast.maketext (Mainactivity.this, "Gender is : "+ Sex[which], toast.length_short). Show (); } }); Builder.setpositivebuttOn ("OK", new Dialoginterface.onclicklistener () {@Override public voi D OnClick (dialoginterface dialog, int which) {} }); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) { } }); Builder.show (); } });
Note : When the dropdown box pops up, when we select an option, the dialog box does not disappear, and we need to click the action button to make the dialog box disappear .
4. Pop up a lower Lado box
Button4.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.seticon (R.drawable.ic_launcher); Builder.settitle ("hobby"); Final string[] Hobbies = {"Basketball", "football", "tennis", "snooker"}; Set a single-selection drop-down box/** * The first parameter specifies the set of data sets we want to display for the next Lado box * The second parameter represents which options are selected, if NULL , it means that none is selected, and if you want to specify which multi-select option box is selected, you need to pass a boolean[] array in the same length as the first parameter, for example {True, False, False, true}; * The third parameter binds a listener to each multi-option */builder.setmultichoiceitems (hobbies, NULL, new Dialogint Erface. Onmultichoiceclicklistener () {stringbuffer sb = new StringBuffer (100); @Override public void OnClick (Dialoginterface dialog, int which, Boolean isChecked) {if (isChecked) {sb.append (Hobbi Es[which] + ","); } toast.maketext (Mainactivity.this, "hobby:" + sb.tostring (), Toast.length_short). Show (); } }); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) { } }); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) { } }); Builder.show (); } });
We see that the Setmultichoiceitems method is used when setting the Lado box, and the meanings of each parameter are described in the code above.
Similarly, for the next Lado box, when we select one of the options, the dialog box does not disappear, only the action button is clicked .
5. Customize pop-up dialog box
For the custom pop-up dialog, we need to specify a custom layout file ourselves, and we'll give you one of the simplest two edittext to enter the username and password:
Dialog.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent "> <edittext android:id= "@+id/username" android:layout_width= "match_parent" android:layout_height= "Wrap_ Content " android:hint=" username "/> <edittext android:id=" @+id/password " android:layout_ Width= "Match_parent" android:layout_height= "wrap_content" android:layout_below= "@id/username" android:hint= "Password" android:inputtype= "Textpassword"/></relativelayout>
Key code:
Button5.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.seticon (R.drawable.ic_launcher); Builder.settitle ("Please enter user name and password"); Load an XML layout file with Layoutinflater as a View object view view = Layoutinflater.from (Mainactivity.this). Inflate (R. Layout.dialog, NULL); Set our own defined layout file as the pop-up box content Builder.setview (view); Final EditText username = (EditText) View.findviewbyid (r.id.username); Final EditText password = (EditText) View.findviewbyid (R.id.password); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) { String a = Username.gettext (). toString (). Trim (); String B = Password.gettext (). toString (). Trim (); Print the username and password you entered Toast.maketext (Mainactivity.this, "username:" + A + ", Password:" + B, Toast.length_sho RT). Show (); } }); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) { } }); Builder.show (); } });
We see that by customizing the popup, we first need to write an XML layout file, then define our layout in it, we do not need to define the button in the layout file, you can set the action buttons by Alertdialog.builder.
Through view view = Layoutinflater.from (Mainactivity.this). Inflate (r.layout.dialog, NULL); We can load our layout file, get a View object, and then set up our custom pop-up box by Alertdialog.builder's Setview method.
Summary: To here, basically will alertdialog detailed explanation, this essay mainly explains the basic concept of dialog pop-up box and detailed explanation alertdialog this pop-up box, including the definition of a basic pop-up warning box, drop-down list box, lower Lado box and so on. Follow-up essays will continue to be recorded to learn about Android's dots and drops.