Disable positive / negative button in DialogFragment

I emulated what I thought was pretty standard Dialogcode:

public class DChooseSeparator extends DialogFragment
{
    // ...
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        builder
            .setTitle("My Title")
            .setView(myDialogLayout)
            .setPositiveButton(getString(R.string.sOKButton), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    if(myEditText.getText().toString().equals("")) // disable positive button if this is empty
                    {
                        Toast.makeText(getActivity(), "enter something!", Toast.LENGTH_SHORT).show();
                    }
                    else { myListener.onSet(myEditText.getText().toString()); }
                }
            })
            .setNegativeButton(getString(R.string.sCancelButton), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // do nothing
                }
            });

        return builder.create();
    }
}

And in onStart Fragment, which shows this:

sepButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        MyDialog myDialog = new MyDialog();
        myDialog.show(getFragmentManager(), "tMyDialogTag");
        myDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);   // DOES NOT WORK
    }
}

However, this does not work, since the function is getButtonnot available for mine DialogFragment. I also cannot do this in the class DialogFragment, as I need to first show().

So ... where exactly can / should it be disabled Button? Do I really need to move the entire method creation Dialogto a method onClick?

Thank you in advance for your help.

+4
source share
2 answers

Button , FragmentDialog. onStart() .

:

public class DChooseSeparator extends DialogFragment
{
    // MEMBER
    private AlertDialog dialog;
    private static boolean mEnableButton;

    // You need an empty constructor: "All subclasses of Fragment must include a public empty constructor. "  
    // like it described in the Fragment API -> so create a new Insatnce with this static methjod
    public static DChooseSeparator newInstance(boolean enableButton){
        mEnableButton = enableButton;
        return new DChooseSeparator();
    } 
    // ...
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        builder
            .setTitle("My Title")
            .setView(myDialogLayout)
            .setPositiveButton(getString(R.string.sOKButton), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    if(myEditText.getText().toString().equals("")) // disable positive button if this is empty
                    {
                        Toast.makeText(getActivity(), "enter something!", Toast.LENGTH_SHORT).show();
                    }
                    else { myListener.onSet(myEditText.getText().toString()); }
                }
            })
            .setNegativeButton(getString(R.string.sCancelButton), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // do nothing
                }
            });

        dialog = builder.create()

        return dialog;
    }

    @Override
    public void onStart(){
        super.onStart();
        dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(mEnableButton);
    }
}

:

sepButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        MyDialog myDialog = new MyDialog(false);
        myDialog.show(getFragmentManager(), "tMyDialogTag");
    }
}
+7

, , oncreateview dialogfragment

0

Source: https://habr.com/ru/post/1524609/


All Articles