【问题标题】:Android: trying to create a custom dialogAndroid:尝试创建自定义对话框
【发布时间】:2013-01-24 06:20:05
【问题描述】:

我正在尝试创建自定义对话框。当我调用 setOnClickListner 方法时,我得到一个 “类型视图中的方法 setOnClickListner 不适用于参数。”我也遇到了错误:

public void onClick(View v) 

说“new OnClickListener(){} 类型的方法 onClick(View) 必须覆盖或实现超类型方法”。代码:

void ReStartsFamilyFriend()
{
    // custom dialog
    // custom dialog
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.gameover);
    dialog.setTitle("Title...");

    // set the custom dialog components - text, image and button


    Button dialogButton = (Button) dialog.findViewById(R.id.butPlay);
    // if button is clicked, close the custom dialog

        //on this line below get the error "The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments "
    dialogButton.setOnClickListener(new OnClickListener() {
        @Override
        /*On the line below I get the error
        "The method onClick(View) of type new OnClickListener(){} must override or implement a supertype 
        method"*/
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog.show();
}   

【问题讨论】:

  • 您是先解散然后再显示吗?这就是它出现在这里的样子。也许只是复制并粘贴确切的代码,而不是在中间解释它,因为它有点模棱两可。
  • 以后请准确复制粘贴您的代码,不要尝试在中间添加解释。如果您想引用特定行,请在该行上或上方添加评论并提及。您发布代码的方式几乎不可能告诉它发生了什么,并且它包含一些看似错误的括号和括号。我试图尽可能地修复格式,但我不愿意猜测解决方案,因为我无法确定我是否理解您所面临的问题。在“不适用于参数”之后有没有说什么?
  • 您是否遇到编译器错误?可能是你没有导入正确的包!

标签: android


【解决方案1】:

我觉得您正在尝试做的只是使用与警报对话框上的按钮不同的按钮来解除警报。您最好的选择是使用 VVVVVVVVVVVVVVVVVv

    dialog.setPositiveButton (CharSequence btnText, new DialogInterface.OnClickListener listener{

    @Override
    onClick(DialogInterface dialogInter, int whichBtn){
        dialog.dismiss();
    }
    });

如果您使用 alertDialog.builder,则无需在视图中放置按钮。
查看link here

【讨论】:

  • 其中 dialog 是您在这段代码之前建立的 AlertBuilder,但绝对应该是 alertDialog.builder。然后,当您完成放置选项时,您可以直接使用dialog.show();,也可以使用dialog.create();,它会为您生成一个警报对话框,然后您可以显示它。前者更容易,所以我建议
猜你喜欢
  • 2011-03-24
  • 1970-01-01
  • 2013-12-16
  • 2019-11-15
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多