在DialogFragment中禁用正/负按钮

Mar*_*tar 5 android android-fragments android-dialog android-dialogfragment

我模仿我认为相当标准的Dialog代码:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

而在onStartFragment,显示它:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用,因为该getButton功能不适用于我DialogFragment.我也不能在DialogFragment课堂上这样做,因为我show()首先需要它.

那么......我究竟能够/应该禁用Button哪些?我是否真的必须将整个创建方法Dialog移到onClick方法中?

预先感谢您的帮助.

owe*_*owe 7

您可以在创建Button视图后启用或禁用FragmentDialog.所以你必须在onStart()Dialog 的方法中调用它.

看我的代码:

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's 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在你可以像这样调用你的Dialog:

sepButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        MyDialog myDialog = new MyDialog(false);
        myDialog.show(getFragmentManager(), "tMyDialogTag");
    }
}
Run Code Online (Sandbox Code Playgroud)