Example tutorial of using dialogfragment to write dialog box in Android

Android provides alert, prompt, pick list, single selection, multiple selection, progress, time picker and date picker dialog boxes, and provides custom dialog boxes. After Android 3.0, dialog is based on fragment and provides compatible support libraries for previous versions, that is, for developers, dialog is based on dialogfragment, but relevant compatible libraries need to be added to the application at this time. Unlike windows or JS dialog, Android dialog is asynchronous rather than synchronous. For a synchronized dialog, after the dialog is displayed, the next line of code will wait until the end of the dialog, that is, the next line of code can know the input of the dialog and the button clicked by the user. For asynchronous dialog, after the dialog is displayed, the next line of code continues to execute, instead of waiting for the dialog to disappear and processing dialog events through callback. Asynchronous dialog also means that the application code can also close the dialog.

Using dialogfragment to manage dialog boxes can better manage its declaration cycle when rotating the screen and pressing the back key. It has basically the same declaration cycle as fragment. And dialogfragment also allows developers to reuse dialog as an embedded component, similar to fragment (different effects can be displayed on large and small screens). The above will show these benefits through examples ~ using dialogfragment requires at least the oncreateview or oncreatedialog methods. Oncreateview displays the dialog using the defined XML layout file. Oncreatedialog creates a dialog using alertdialog or dialog.

Let's take a look at an example of using dialogfragment to write a dialog: 1. Rewrite oncreateview to create a dialog a) layout file. We create a layout file with a set name:

b) Inherit dialogfragment and override oncreateview method

C) test run: invoked in the Main method:

design sketch:

You can see that the dialog box was created and displayed successfully, but there is a dislike title in the default dialog box. How do we remove it? We can call getDialog ().RequestWindowFeature (Window.FEATURE_NO_TITLE) in onCreateView. Can be removed. Namely:

design sketch:

Perfectly removed the annoying title.

2. Rewrite oncreatedialog to create a dialog. In oncreatedialog, you can generally use alertdialog or dialog to create a dialog. However, since Google does not recommend using dialog directly, we will use alertdialog to create a login dialog. a) Layout file

b) Inherit dialogfragment and override oncreatedialog method

c) Call

design sketch:

You can see that you can also create dialog boxes by rewriting oncreatedialog, and the effect is still very nice.

3. Transfer data to activity. You can use the "fragment interface pattern" method to transfer data from the dialog to activity. The login box above is modified to show this mode. The change is relatively small, and the code is pasted directly:

Get the reference of username and password. When you click login, force the activity into our custom interface: logininputlistener, and then return the data entered by the user. Mainactivity needs to implement our interface logininputlistener and our method to obtain our account password when users click login:

c) MainActivity

effect:

4. Dialogfragment for screen adaptation, we hope that a dialog box will be displayed in the form of a dialog box on the large screen, while the small screen will be directly embedded in the current actvity. The dialog box with this effect can only be realized by overriding oncreateview. Next, we use the above editnamedialogfragment to display. Editnamedialogfragment has been written, and the call is written directly in mainactivity

As you can see, we read r.bool.large_ Layout, and then according to the obtained Boolean value, if it is a large screen, it will be directly displayed in a dialog box; if it is a small screen, it will be embedded in the r.bool.large in our activity layout_ Layout is the resource file we define: create a new bools.xml file under the default values

Then create a values large under res and a bools.xml under values large

Final test:

The simulator is on the left and my mobile phone is on the right~~~~~

5. Screen rotation when the user enters the account password, he suddenly rotates the screen, and the account password is missing ~ ~ ~ is it crazy? When the traditional new alertdialog rotates, first, the value entered by the user will not be saved, and second, an exception will be reported, because the dialog box is not allowed to be closed before the activity is destroyed. The dialog box realized by dialogfragment can completely avoid the problem of rotation. We directly copy the login box created by alertdialog to mainactivity and call it directly:

Next, I click the login box created in two ways to see the effect picture:

You can see that the traditional dialog disappears when rotating the screen, and the background log will report an exception ~ ~ ~ using dialogfragment will not be affected.

PS: let's talk about fragment manager again. Through fragment manager or fragment transaction, we can specifically control dialog fragments. Show () is to add fragments to the manager, and disass () is to remove fragments from the manager. We cannot add () first and then show (), so a fragment object can only be added to the manager once. If the fragment is disassiss (), it will be deleted from the manager. We can no longer obtain the information of the fragment through the manager. Therefore, if we want to keep some status or information of the disassiss dialog, we need to save it outside the dialog, such as using activity. Summary: programming idea: encapsulate the interface. In a small example, the fragment will call ondialogdone() of the activity to display toast and other information. In a real project, it is not necessary to understand various methods of activity for the preparation of fragment. A good programming style is to encapsulate the methods involved in fragment in the form of interface, as follows:

In activity, add the interface implementation as follows:

Accordingly, in the fragment, the call to this method can be written as:

For some large projects, if we can't determine whether the activity really implements the interface, we can detect it in the early stage of the fragment, that is, the stage when the activity is just associated, as follows:

Fragment and activity communicate with other fragments: the small example demonstrates the communication between them by obtaining the interface object through getactivity () or directly obtaining the object of activity. In addition, fragments can also obtain other fragment instances through the fragment manager and tag, so as to communicate between fragments. Of course, from the perspective of programming ideas, too many cross calls between fragments are not conducive to program control.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>