45

我已经写了一个DialogFragment。现在我意识到我希望它像 AlertDialog 一样有一个正面和一个负面按钮。如何在维护我编写的代码的同时实现这样的目标?

public class DoublePlayerChooser extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        

    setStyle(DialogFragment.STYLE_NORMAL,0);



}

@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();
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.doubleplayerchooser, container, false);
    getDialog().setTitle("Enter Players");

    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);



    return v;
}
4

5 回答 5

72

这就是我想出来的。我删除了 onCreateView 并更改了 onCreateDialog。这个链接实际上有答案,所以所有的功劳都应该去那里。我刚刚发布了它,以防有人首先遇到这个问题。

    @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();
}
于 2013-09-03T21:41:58.017 回答
7

您必须覆盖 DialogFragments onCreateDialog(...) 方法:

@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();
}

取自这里:Android:禁用 DialogFragment OK/Cancel 按钮

根据您收到的错误消息(“必须调用请求功能...”),我建议:

不要在 Activity 中的 requestFeature() 之前调用 setContentView() 或在任何地方调用它。

此外:

不要在 onCreate() 中调用setStyle(... )。

在你创建 Fragment 的地方调用它。

YourDialogFragment f = new YourDialogFragment(Context);
f.setStyle(...);
// and so on ...
于 2013-09-03T20:39:09.673 回答
4

最清晰的方法。

// 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();
}
于 2016-04-10T21:08:06.680 回答
1

要添加操作按钮,请调用setPositiveButton()setNegativeButton()方法:

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

更多关于 DialogFragment的信息在这里

于 2013-09-03T20:45:42.960 回答
1

这有点旧,但最近我onCreateView在扩展时一直在覆盖AppCompatDialogFragment。只需将您自己的按钮放在与您返回的onCreateView使用样式相同的布局中,例如@style/Widget.AppCompat.Button.Borderless.

当单击操作按钮时,您可以获得控制对话框自动关闭的额外好处,特别是因为这些自定义视图有时需要输入,并且您希望在单击按钮时阻止对话框自动关闭。

使用自定义视图onCreateDialog总是让人觉得很脏,因为你在没有容器的情况下给它充气。AlertDialog.BuilderGoogle 尝试使用新的 v7方法使 API 更好一些setView(int layoutResId),但您无法调用findViewById

您应该在您的 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>

您也必须onCreateDialog在 DialogFragment 中覆盖才能返回new AppCompatDialog(getActivity(), R.style.AlertDialog)

于 2017-03-28T05:04:13.380 回答