【问题标题】:AlertDialog - Trying to get the Alert to workAlertDialog - 试图让警报工作
【发布时间】:2011-09-09 16:52:14
【问题描述】:

所以我在这里搜索了一些关于 AlertDialog 的问题,但我不完全确定我在做什么,所以我很难将这些问题与我自己的示例联系起来。 (我对整个 android 编程还是新手,所以请多多包涵。)

我已经在公共类_下定义了这个Activity实现了OnCLickListener ...

    public AlertDialog myAlertDialog;

然后我在 onClick 下有这个

    public void onClick(View src) {

    switch(src.getId()){
    case R.id.buttonOk:
        if (score==0){
            AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
            myAlertDialog.setTitle("Title");
            myAlertDialog.setMessage("Message");
            myAlertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                    return;
                } }); 
            myAlertDialog.show();
        }

此行及其下一行有错误:

myAlertDialog.setButton("OK", new DialogInterface.OnClickListener() {

错误:

1st:此行有多个标记 - DialogInterface 无法解析为类型 - 方法 setButton(String, new OnClickListener(){}) 未定义类型

第二个:DialogInterface 无法解析为类型

谁能告诉我我做错了什么?

谢谢!

【问题讨论】:

    标签: android android-layout dialog alert android-alertdialog


    【解决方案1】:

    我很确定您只是没有导入 DialogInterface。尝试将此语句添加到代码的开头。

    import android.content.DialogInterface;
    

    【讨论】:

    • 或按 ctrl+shift+O(“哦”不为零)
    • 很好用,我会尽快接受答案,但你能帮我做一件事吗?它现在说 The method setButton(String, new DialogInterface.OnClickListener(){}) is undefined for type AlertDialog.Builder 在第二行。
    • 从头开始,只需将其更改为 setPotitiveButton 即可!再次感谢大家...爱这个社区!
    【解决方案2】:

    我认为你需要setPositiveButton()setNegativeButton() AlertDialog.Builder 上没有方法setButton()

    并确保导入DialogInterface

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 2019-02-01
    • 2011-04-13
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多