向DialogFragment的对话框添加正/负button

嗨,我已经写了一个DialogFragment。 现在我已经意识到,我希望它有一个正面和负面的button就像一个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; } 

Solutions Collecting From Web of "向DialogFragment的对话框添加正/负button"

好吧,这是我怎么想的。 我抹去了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(); } 

您必须重写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确定/取消button

根据你所得到的错误信息(“请求function必须被称为…”)我会build议:

不要在你的Activity的requestFeature()之前调用setContentView(),也不要调用它。

此外:

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

把它叫做你创build片段的地方。

 YourDialogFragment f = new YourDialogFragment(Context); f.setStyle(...); // and so on ... 

要添加操作button,请调用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的更多信息,请点击这里 。

最清晰的方式。

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

这是onCreateView ,但最近我已经覆盖onCreateView扩展AppCompatDialogFragment 。 只需将您自己的button放入您在onCreateView返回的相同布局 – 使用类似@style/Widget.AppCompat.Button.Borderless

当点击一个动作button时,你会得到额外的控制对话框自动解除的好处,特别是因为这些自定义视图有时需要input,而且你想在单击一个button时阻止对话框的自动closures。

onCreateDialog使用自定义视图一直觉得脏,因为你没有容器充气。 谷歌尝试使用新的v7 AlertDialog.Builder方法setView(int layoutResId)更好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> 

您必须重写DialogFragment中的onCreateDialog以返回new AppCompatDialog(getActivity(), R.style.AlertDialog)