Android UI series —– dialog dialog example
In Android development, we often need to pop up a dialog box on the interface. This essay will explain the concept of dialog dialog box in detail, including defining different styles of dialog boxes.
1、 Dialog
Let's first look at the introduction of dialog in the official Android document
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.
Dialog is a dialog box that pops up on the screen and allows users to make a choice or enter additional information. A dialog box will not cover our whole screen, and it is usually used in model events. Users need to make a decision before continuing.
Dialog class is the base class of dialog dialog box, but we should avoid directly using this class to instantiate a dialog box. We should use its subclass to get a dialog box:
java.lang.Object ↳ android.app.Dialog
KNown Direct Subclasses
AlertDialog,CharacterPickerDialog,MediaRouteChooserDialog,MediaRouteControllerDialog,Presentation
KNown Indirect Subclasses
DatePickerDialog,ProgressDialog,TimePickerDialog
We can see that dialog has many subclass implementations, so we need to define a dialog and instantiate one with its subclass instead of directly using the parent class dialog.
2、 Alertdialog
Today, we focus on the alertdialog dialog. We can see that alertdialog is a direct subclass of dialog.
Using alertdialog, we can display a title, up to 3 button actions, and a set of selection boxes or self-defined pop-up boxes.
Here we borrow a diagram provided in the official Android documentation to see the composition of the alertdialog box:
① Area 1 defines the header information of the pop-up box, including the title name or an icon.
② Area 2 is the content part of the alertdialog. Here we can set some message messages, define a group of selection boxes, and define our own layout pop-up boxes.
③ Area 3 makes our action buttons section, where we can define our action buttons.
When it comes to action buttons, you should pay special attention to:
In the alertdialog, buttons are defined through the setxxxbutton method. There are three different action buttons for us to choose from:
1. Setpositivebutton (charsequence text, dialoginterface. Onclicklistener listener) this is a button equivalent to OK and confirm the operation.
2. Setnegative button (charsequence text, dialoginterface. Onclicklistener listener) this is a button equivalent to canceling the operation.
3. Setneutralbutton (charsequence text, dialoginterface. Onclicklistener listener) is equivalent to a button that ignores operations.
We can only have one action button at most, that is, only one positive button can appear in the pop-up dialog box.
Next, let's take a look at several commonly used alertdialog dialogs through a specific example.
1. A warning box pops up and there are three buttons to select
Let's look at the code section:
If we want to create an alertdialog, we need to use an internal class of alertdialog, alertdialog. Builder, to build an alertdialog, and then set the content we want to display through the setxx method.
We can see that we have set three action buttons in total. Each button is bound with a listening event of dialoginterface. Onclicklistener(). Then we pop up some information through the toast dialog box (which will be explained in the following essay). The which method represents the int value represented by the action button:
positive: -1
negative: -2
neutral: -3
We can know that which = - 1 means you click the OK button, - 2 means you click the Cancel button, and - 3 means you click the ignore button.
2. Drop down list pop-up box
The key codes are as follows:
Here, we set a drop-down list box through the setitems (charsequence [] items, dialoginterface. Onclicklistener listener) method. Note: because drop-down list boxes or drop-down multiple selection boxes are displayed in content, message and drop-down list boxes cannot exist at the same time.
We can also bind a dialoginterface. Onclicklistener listener to it. When an option is selected, the dialog box will disappear. Which here represents the index of each option in the drop-down list. Through this, we can easily get which option the user selected.
3. A drop-down radio box pops up
Note: when the drop-down radio box pops up, when we select an option, the dialog box will not disappear. We need to click the action button to make the dialog box disappear.
4. A drop-down multiple selection box pops up
We can see that the setmultichoiceitems method is used when setting the drop-down multiple selection box. The meaning of its parameters has been described in the above code. Similarly, for the drop-down multiple selection box, when we select one of the options, the dialog box will not disappear. It will not disappear until we click the action button.
5. Customize pop-up dialog box
For the user-defined pop-up dialog box, we need to specify a user-defined layout file. We give the simplest two edittexts for entering user name and password:
dialog.xml
Key codes:
We can see that by customizing the pop-up box, we first need to write an XML layout file, and then define our layout in it. We don't need to define button buttons in the layout file. We can set action buttons through alertdialog.builder.
Through view view = layoutinflator.from (mainactivity. This). Inflate (r.layout.dialog, null); We can load our layout file to get a view object, and then set our custom pop-up box through the setview method of alertdialog. Builder
Summary: here, we have basically finished the detailed explanation of alertdialog. This essay mainly explains the basic concept of dialog pop-up box and alertdialog pop-up box in detail, including defining a basic pop-up warning box, drop-down list box, drop-down multi-choice box, etc. Subsequent essays will continue to record every bit of learning Android.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.