In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use DialogFragment. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
01 Overview
DialogFragment was introduced in Android 3.0 so that dialog was also fragmented. DialogFragment is a subclass of Fragment and is used to create dialog boxes for user interaction. Before DialogFragment falls to the ground, we usually use AlertDialog and Dialog to create dialogs. Currently, Google officially does not recommend using Dialog to create dialogs directly.
02 advantages
The life cycle of DialogFragment is consistent with that of Fragment. It also has its own window that can handle events that accept user input and destroy controls (by pressing the return key or the user clicks the cancel key of the dialog box). Therefore, it is convenient for Activity to better control and manage DialogFragment.
Compared to the problem that Dialog,DialogFragment can perfectly switch between horizontal and vertical screens. The Dialog disappears as the screen switches, and the DialogFragment automatically resizes with the screen, and you can even save the user's state before the switch by sending Bundle to Fragment.
The Dialog control does not fall within the scope of Activity management, so there is no need to write a lot of code about Dialog in Activity, it should be extracted.
03 usage
Inherit the DialogFragment class, and then you must implement the onCreateView (LayoutInflater, ViewGroup, Bundle) or onCreateDialog (Bundle) methods. OnCreateView displays Dialog even if it uses a defined xml layout file. OnCreateDialog creates a Dialog using AlertDialog or Dialog.
1) create a custom Dialog
Create a layout file named show_version_basicdialog, which is the layout file corresponding to DialogFragment.
B. Inherit the DialogFragment class and override the onCreateView method
There are only two methods in showVersionDialogFragment, one newInstance (), to get an instance of showVersionDialogFragment. The other is to use inflater to load the layout you just created.
In the example samples-23_r02\ content\ webview\ PermissionRequest, Google officially suggested that we provide a method of newInstance () in our dialog box. This is easy to manage, for example, if you need the dialog box to be at the top of the stack all the time, you need to use FragmentTransaction combined with newInstance () to control it.
C. Display the dialog box
The show () method takes two parameters, the first of which is passed in the FragmentManager object, and the second is a Tag, as long as it is not repeated with the others. With this tag,FragmentManager, you can get an example of the dialog box through findFragmentByTag.
2) create an Alert Dialog
Inherit DialogFragment and override the onCreateDialog method
This method returns an AlertDialog instantiated object.
B, display the dialog box
04 interact with Activity (Framgent)
Let's continue with the example of creating Alert Dialog above.
The simplest and most convenient way is to use Java's interface callback mechanism to define a callback interface in the DialogFragment class, which is implemented by Activity (Framgent).
In the DialogFragment class
In the Activity (Framgent) class
The above is how to use DialogFragment, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.