Custom Dialog Fragment

前端 未结 2 1789
灰色年华
灰色年华 2021-01-13 04:34

I am trying to create a simplistic dialog similar to a DatePickerDialog. The Dialog that I am creating should provide the user with an array of ima

2条回答
  •  渐次进展
    2021-01-13 05:04

    If the activity in which you want to open the DialogFragment extends FragmentActivity, you should execute:

    PicturePickerFragment dialog = new PicturePickerFragment();
    dialog.show(getSupportFragmentManager(), "YourDialog");
    

    Also you need to inflate your dialog layout file in the method onCreateDialog of your dialog fragment class.

    Using AlertDialog.Builder you can achieve all this easily, e.g.

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
    LayoutInflater inflater = getActivity().getLayoutInflater();
    
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialoglayout, null))
     .setTitle(R.string.dialog_title)
     .setPositiveButton(R.string.pos_button, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // call the method on the parent activity when user click the positive button
        }
    })
     .setNegativeButton(R.string.neg_button, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // call the method on the parent activity when user click the negative button
        }
    });
    return builder.create();
    

    There are many examples on http://developer.android.com/reference/android/app/DialogFragment.html

提交回复
热议问题