This article describes the method of creating a dialog box based on DialogFragment in Android. Share it for your reference, as follows:
/** * Create a dialog using DialogFragment * @description: * @author ldm * @date 2016-5-12 2:00:01 pm */ public class FragmentAlertDialog extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.fragment_dialog); // Initialize Button and set up listening button = (Button) findViewById(); (new OnClickListener() { public void onClick(View v) { // A dialog box pops up showDialog(); } }); } void showDialog() { // DialogFragment Create dialog box DialogFragment newFragment = MyAlertDialogFragment .newInstance(.alert_dialog_two_buttons_title); (getFragmentManager(), "dialog"); } public void doPositiveClick() { ("FragmentAlertDialog", "Positive click!"); } public void doNegativeClick() { ("FragmentAlertDialog", "Negative click!"); } /** * Custom pop-up dialog DialogFragmet * * @description: * @author ldm * @date 2016-5-12 1:54:31 pm */ public static class MyAlertDialogFragment extends DialogFragment { public static MyAlertDialogFragment newInstance(int title) { MyAlertDialogFragment frag = new MyAlertDialogFragment(); Bundle args = new Bundle(); ("title", title); (args); return frag; } /** * DialogFragment needs to implement onCreateView or onCreateDIalog methods. * onCreateView(): Display Dialog using the defined xml layout file. * onCreateDialog(): Create Dialog using AlertDialog or Dialog. */ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { int title = getArguments().getInt("title"); return new (getActivity())//Create a Dialog .setIcon(.alert_dialog_icon)//Set icon .setTitle(title)//Set the title .setPositiveButton(.alert_dialog_ok, new () {//Confirm (OK) button public void onClick(DialogInterface dialog, int whichButton) { ((FragmentAlertDialog) getActivity()) .doPositiveClick(); } }) .setNegativeButton(.alert_dialog_cancel,//Cancel button new () { public void onClick(DialogInterface dialog, int whichButton) { ((FragmentAlertDialog) getActivity()) .doNegativeClick(); } }).create(); } } }
Layout file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="4dip" > <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:layout_weight="1" android:gravity="top|center_horizontal" android:text="Example of displaying an alert dialog with a DialogFragment" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0" android:text="@string/show" > </Button> </LinearLayout>
Open source code: /ldm520/ANDROID_API_DEMOS
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.