如何在Android中使用showAlert方法?

jkm*_*gee 5 android

我正在尝试调试某些内容,并希望弹出一个消息对话框.Eclipse告诉我它要"创建方法showAlert(string,string,string,boolean)"

我导入了这个导入android.content.DialogInterface;

我错过了什么步骤?

MrS*_*ake 6

如果您尝试创建并显示AlertDialog,则应该使用AlertDialog.Builder.

DialogInterface,顾名思义,是一个接口,只有2个方法:cancel()和dismiss().

创建AlertDialog非常简单:

new AlertDialog.Builder(this)
.setTitle("Some Title")
.setMessage("some message")
.setPositiveButton("OK", new OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) {
        // Some stuff to do when ok got clicked
    }
})
.setNegativeButton("cancel", new OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) {
        // Some stuff to do when cancel got clicked
    }
})
.show();
Run Code Online (Sandbox Code Playgroud)

这显示了一个简单的AlertDialog.

提示:检查Activity.showDialog(int)和Activity.onCreateDialog(),它们使您在使用对话框时的生活更轻松.


Die*_*ano 5

如果您只显示调试消息,可以尝试Toast.makeText():

Toast.makeText(context, "Hi there!", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)

别忘了打电话show().


Jus*_*tin 0

看来您的参数类型可能不匹配。检查您的参数实际上是字符串还是布尔值。也许您需要调用toString()您的对象?