Adding positive / negative Button to DialogFragment's Dialog

断了今生、忘了曾经 提交于 2019-11-28 07:10:55
Libathos

This is how I figured it out. I erased the onCreateView and altered the onCreateDialog. This link actually had the answer so all the credit should go there. I've just posted it just in case anyone bumps in this question first.

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder b=  new  AlertDialog.Builder(getActivity())
    .setTitle("Enter Players")
    .setPositiveButton("OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // do something...
            }
        }
    )
    .setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
            }
        }
    );

    LayoutInflater i = getActivity().getLayoutInflater();

    View v = i.inflate(R.layout.doubleplayerchooser,null);

    firstPlayerPicker =  (ImageButton) v.findViewById(R.id.imageButton1);
    firstPlayerPicker.setOnClickListener(new OnClickListener() {
        public void onClick(final View v){

            callContactPicker(1);

        }       
    });

    secondPlayerPicker =  (ImageButton) v.findViewById(R.id.ImageButton01);
    secondPlayerPicker.setOnClickListener(new OnClickListener() {
        public void onClick(final View v){

            callContactPicker(2);

        }       
    });

    loadFromFile =  (ImageButton) v.findViewById(R.id.imageButton2);
    loadFromFile.setOnClickListener(new OnClickListener() {
        public void onClick(final View v){



        }       
    });

    firstTextfield =  (EditText) v.findViewById(R.id.editText1);
    secondTextfield =  (EditText) v.findViewById(R.id.EditText01);

    firstImage = (ImageView) v.findViewById(R.id.imageView1);
    secondImage = (ImageView) v.findViewById(R.id.ImageView01);


    b.setView(v);
    return b.create();
}
Philipp Jahoda

You have to override the DialogFragments onCreateDialog(...) method:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    return new AlertDialog.Builder(getActivity())
            .setTitle("title")
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // do something...
                    }
                }
            )
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                }
            )
            .create();
}

Taken from here: Android: disable DialogFragment OK/Cancel buttons

According to the error message you are getting ("request feature must be called...") I would recommend:

Don't call setContentView() before requestFeature() in your Activity or wherever it is you are calling it.

Furthermore:

Dont call setStyle(...) inside the onCreate().

Call it where you create your Fragment.

YourDialogFragment f = new YourDialogFragment(Context);
f.setStyle(...);
// and so on ...

To add action buttons call the setPositiveButton() and setNegativeButton() methods:

public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.dialog_fire_missiles)
           .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // FIRE ZE MISSILES!
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    // Create the AlertDialog object and return it
    return builder.create();
}
}

More information about DialogFragment here.

The clearest way.

// Your own onCreate_Dialog_View method
public View onCreateDialogView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.your_layout, container); // inflate here
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // do something with 'view'
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // setup dialog: buttons, title etc
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity())
            .setTitle(R.string.dialog_fragment_author_title)
            .setNegativeButton(R.string.dialog_fragment_author_close,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.dismiss();
                        }
                    }
            );

    // call default fragment methods and set view for dialog
    View view = onCreateDialogView(getActivity().getLayoutInflater(), null, null);
    onViewCreated(view, null);
    dialogBuilder.setView(view);

    return dialogBuilder.create();
}

This is a bit old but lately I've been overriding onCreateView when extending AppCompatDialogFragment. Just put your own buttons in the same layout you return in onCreateView - use styles like @style/Widget.AppCompat.Button.Borderless.

You get the added bonus of controlling the Dialog self-dismissing when an action button is clicked, especially since these custom views sometimes have required inputs and you want to block the auto-close of the Dialog when a button is clicked.

Using a custom view in onCreateDialog has always felt dirty because you're inflating it with no container. Google tried making the API a bit nicer with the new v7 AlertDialog.Builder method setView(int layoutResId), but you can't call findViewById then.

You should be adding a Theme like this in your styles.xml:

<style name="AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorPrimary">@color/material_light_blue_500</item>
    <item name="colorPrimaryDark">@color/material_light_blue_800</item>
    <item name="colorAccent">@color/material_light_blue_a400</item>
    <item name="colorButtonNormal">@color/material_light_blue_500</item>
    <item name="colorControlNormal">@color/material_light_blue_600</item>
    <item name="colorControlActivated">@color/material_light_blue_a100</item>
    <item name="colorControlHighlight">@color/material_light_blue_a100</item>
</style>

You must override onCreateDialog in your DialogFragment to return new AppCompatDialog(getActivity(), R.style.AlertDialog) as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!