在DialogFragment中禁用正/负按钮

时间:2014-01-30 22:00:38

标签: 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();
    }
}

在展示它的onStart的{​​{1}}中:

Fragment

但是,这不起作用,因为我的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 } } 无法使用getButton功能。我也不能在DialogFragment课程中执行此操作,因为我需要首先DialogFragment

那么......在哪里可以/应该禁用show()?我是否真的必须将Button的整个创建移至Dialog方法?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:7)

您可以在创建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'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);
    }
}

现在您可以像这样调用Dialog:

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

答案 1 :(得分:0)

你需要在创建对话框的视图后,在dialogfragment类的oncreateview函数中调用它

相关问题