java - 警报对话框中的上下文

标签 java android this android-alertdialog

这是我正在使用的警报对话框的代码:

new AlertDialog.Builder(AlertDemo.this)
    .setTitle("This is Alert Demo")
    .setMessage("Here is an Alert Message!")
    .setNeutralButton("Close", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dlg, int sumthin) 
        {
            // do nothing – it will close on its own
        }
    })
    .show();    

当我写 this 而不是 AlertDemo.this 时,它显示错误 The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined .. 这是什么意思? thisAlertDemo.this 有什么区别?

最佳答案

如果简单的使用this,它就是内部类View.onClickListener的一个实例

为了在点击按钮时显示警报对话框,您需要传递 AlertDemo 类的实例。

这就是您使用 AlertDemo.this

的原因

关于java - 警报对话框中的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12263471/

相关文章:

java - 在JAVA中解析xml响应

java - 如何缩短使用相似代码的不同场景的算法?

android - 使用多个安卓模拟器执行 WIFIDirectDemo 示例

java - 使用 MifareUltralight.transceive 在 Android 中设置 NTAG213 密码崩溃,没有抛出异常

C# 关键字类似于 "this"但对于当前类类型?

javascript - 在 Javascript 中将对象原型(prototype)函数绑定(bind)到对象

JavaFx 和 float 按钮

java - 即使故意使用不正确的 keystore 密码,APK 也会构建

android - 使用 Libgdx 为 64 位设备更新 Android 应用程序

java - 如何从回调函数中调用类变量?