Android 故障设置对话框正按钮

Android 故障设置对话框正按钮,android,android-xml,android-dialog,Android,Android Xml,Android Dialog,我将对话框创建为: // custom dialog Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.add_taste_dialog); dialog.setTitle("Add Taste"); 然后,我尝试使用以下选项设置肯定按钮: dialog.setPositiveButton(R.string.addtaste, new DialogInterface.OnClickListener()

我将对话框创建为:

// custom dialog
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);       
dialog.setTitle("Add Taste");
然后,我尝试使用以下选项设置肯定按钮:

dialog.setPositiveButton(R.string.addtaste, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {




            }
        });
Eclipse给我这个错误:

The method setPositiveButton(int, new DialogInterface.OnClickListener(){}) is undefined for the type Dialog
我在这里参考了android开发者的参考资料:


如错误消息所示,未为
对话框
类定义
设置正按钮
方法。但是,它是为
AlertDialog.Builder
类定义的:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add Taste")
builder.setPositiveButton(R.string.addtaste, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          //code goes here
        }
}
AlertDialog dialog = builder.create()
如果您提供给对话框的布局超出了标准的
AlertDialog
所能容纳的范围,那么您可以将当前代码与dialog类的
findViewById(int id)
方法结合使用来查找按钮,前提是您在添加的布局中包含了一个按钮。否则,可以使用
addContentView(视图视图,ViewGroup.LayoutParams参数)
方法添加按钮

以下是dialog类研究这些方法的参考:


祝你好运

如果使用自定义xml布局进行对话框。那你为什么不在你的自定义布局中加入积极的按钮呢?只需将按钮放在对话框xml文件中,并在其单击事件中执行这些操作

Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

可能您需要为此提供完整的代码。如果我使用对话框生成器,我仍然可以将内容视图设置为自定义xml吗?setContentView(R.layout.add\u taste\u对话框)@Mike您可以添加
您的\u自定义\u xml
但需要另一个调用
setView(您的\u充气\u视图)
其中
查看您的\u充气\u视图=充气器。充气(R.layout.your\u自定义\u xml,root)
。我想你可以单独使用充气机。我得到了这个错误:类型视图中的方法setOnClickListener(View.OnClickListener)不适用于参数(new OnClickListener(){})