android - AlertDialog - 试图让警报工作

标签 android android-layout dialog alert android-alertdialog

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

我已经在公共(public)类下定义了这个 _ Activity implements 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() {

错误:

第一个:这条线上有多个标记 - DialogInterface 无法解析为类型 - 方法 setButton(String, new OnClickListener(){}) 未定义类型

第二:DialogInterface 无法解析为类型

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

谢谢!

最佳答案

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

import android.content.DialogInterface;

关于android - AlertDialog - 试图让警报工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6259358/

相关文章:

android - 在 2 个不同的回调中调用一个 Activity 中的多个布局

带有颜色资源 : What am I doing wrong? 的 Android LinearLayout

facebook - 必须使用 session key 调用 Iframe 对话框

android - 无法显示警报对话框

Android ListView如何禁用多点击

Android View setPadding() 与 setPaddingRelative()

android - 存储内容类型和子内容的数据库架构(ANDROID _ SQLITE :)

java - JSOUP:无法解析方法标题()

java - Android 中增强现实应用程序的离线 map

android - 如何更改android :layout_alignParentRight runtime by java code