这是我正在使用的警报对话框的代码:
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
.. 这是什么意思? this
和 AlertDemo.this
有什么区别?
最佳答案
如果简单的使用this
,它就是内部类View.onClickListener的一个实例
为了在点击按钮时显示警报对话框,您需要传递 AlertDemo 类的实例。
这就是您使用 AlertDemo.this
关于java - 警报对话框中的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12263471/