3

解决方案是 onCreatDialog。关于崩溃的另一个问题是由我的 listView xml 中的 LinearLayout 父级引起的

这是我的代码[编辑]:

public class MyDialogFragment extends DialogFragment {

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

        View mView = inflater.inflate(R.layout.fragment_dialog, null);
        ListView mListView = (ListView) mView.findViewById(R.id.lvListe);

        String[] mValues = {"Orientation", "Nombre de Chambres", "Nombre de Salle de bains", "Nombre de toilettes", "Cave", "Parking", "Garage", "Jardin"};

        getDialog().setTitle("Autres Critères");

        mListView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mValues));

        return mView;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new AlertDialog.Builder(getActivity())
                .setPositiveButton("ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                }
                        }
                )
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                onCancel(dialog);
                            }
                        }
                )
                .create();
    }

}

提前致谢 :)

4

3 回答 3

3

Please take a look at the createDialog() method of DialogFragment.

Here is an example:

@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) {
                        // DO SOMETHING
                    }
                }
            )
            .create();
}
于 2013-08-23T14:28:18.810 回答
1

Try using the AlertDialog.Builder like such:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    Bundle args = getArguments();

    builder.setTitle("Title");
    builder.setMessage(args.getString(ARG_MESSAGE));
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            //do stuff here
        }
    });
    //cancel button with dismiss.
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
             dismiss();
        }
    });

    return builder.create();
}
于 2013-08-23T14:28:27.467 回答
0

代替(或除此之外)实现 onCreateView(LayoutInflater, ViewGroup, Bundle) 以在对话框内生成视图层次结构,您可以实现 onCreateDialog(Bundle) 来创建您自己的自定义 Dialog 对象。

这对于创建 AlertDialog 非常有用,允许您向用户显示由片段管理的标准警报。一个简单的示例实现是:

public static class MyDialogFragment extends DialogFragment {


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int title = getArguments().getInt("title");

    return new AlertDialog.Builder(getActivity())
            .setIcon(R.drawable.alert_dialog_icon)
            .setTitle(title)
            .setPositiveButton(R.string.alert_dialog_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ((FragmentAlertDialog)getActivity()).doPositiveClick();
                    }
                }
            )
            .setNegativeButton(R.string.alert_dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ((FragmentAlertDialog)getActivity()).doNegativeClick();
                    }
                }
            )
            .create();
}

}

于 2013-08-23T14:30:49.303 回答