1. OverviewDialogfragment was introduced when Android was 3.0. is a special fragment for displaying a modal dialog box over the content of an activity. Typical for: Display warning boxes, input boxes, confirmation boxes, and so on.
Before dialogfragment, we create dialog boxes: generally using Alertdialog and dialog. Note: The official does not recommend to use dialog to create a dialog box directly.
2. Benefits and UsageUsing Dialogfragment to manage the dialog box, you can better manage its declaration cycle when you rotate the screen and press the back key, which has a basically consistent declaration cycle with fragment. And Dialogfragment also allows developers to reuse dialog as an embedded component, similar to fragment (which can show different effects on large and small screens). The above will show these benefits by example
Using dialogfragment requires at least the implementation of Oncreateview or Oncreatedialog methods. Oncreateview shows dialog using the defined XML layout file. Oncreatedialog uses Alertdialog or dialog to create a dialog. 3. Rewrite oncreateview Create dialog
A) layout file, we create a layout file with the name set:
[HTML] View Plain Copy <?xml version= "1.0" encoding= "Utf-8"?> <RelativeLayout Xmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" > <TextView android:id= "@+id/id_label_your_name" android: Layout_width= "Wrap_content" android:layout_height= " 32DP " android:gravity=" center_vertical android:text= "Your name:" /> <EditText android: Id= "@+id/id_txt_your_name" android:layout_width= " Match_parent " android:layout_height=" Wrap_content " android:layout_torightof= "@id/id_label_your_name" android:imeoptions= "Actiondone" android:inputtype= "text" /> <Button android:id= "@+id/id_ Sure_edit_name " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignparentright= "true" android:layout_below= "@id/id_txt_your_name" android:text= "OK" /> </RelativeLayout>
b inheriting Dialogfragment, overriding Oncreateview methods
[Java] view plain copy package com.example.zhy_dialogfragment;