解决方案是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();
}
}
提前致谢:)
答案 0 :(得分:3)
请查看createDialog()
的{{1}}方法。
以下是一个例子:
DialogFragment
答案 1 :(得分:1)
尝试使用AlertDialog.Builder
之类的内容:
@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();
}
答案 2 :(得分: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();
}
}