【问题标题】:Adding positive / negative Button to DialogFragment's Dialog将正/负按钮添加到 DialogFragment 的 Dialog
【发布时间】:2013-09-07 04:55:58
【问题描述】:

我已经写了一个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;
}

【问题讨论】:

    标签: android android-dialog android-dialogfragment


    【解决方案1】:

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

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

    【讨论】:

    • 如果内容视图仍然在onCreateView(LayoutInflater.from(getActivity()),null,savedInstance)中创建会更清晰。
    【解决方案2】:

    您必须覆盖 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: disable DialogFragment OK/Cancel buttons

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

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

    此外:

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

    在你创建片段的地方调用它。

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

    【讨论】:

    • 我已经这样做了,但是我得到一个异常请求功能必须在添加内容之前调用 android
    • 那么请发布你的一些代码,看看我更新的答案。
    • 当我删除 onCreateView 时,它运行时没有错误。如果这对你有帮助
    • onCreateView 已发布。对于 API,setStyle 不是我的。我已经删除了 setStyle,但它不起作用。
    • 抱歉,您的 onCreateView() 方法未发布。只有 onCreate() 方法。
    【解决方案3】:

    最清晰的方法。

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

    【讨论】:

      【解决方案4】:

      要添加操作按钮,请调用 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的信息here

      【讨论】:

        【解决方案5】:

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

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

        onCreateDialog 中使用自定义视图总是让人觉得很脏,因为您在没有容器的情况下对其进行充气。 Google 尝试使用新的 v7 AlertDialog.Builder 方法 setView(int layoutResId) 使 API 更好一些,但你不能调用 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)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-24
          • 2011-12-20
          • 1970-01-01
          相关资源
          最近更新 更多