如何在Android中使用showAlert方法?

时间:2010-02-01 19:01:58

标签: android

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

我已导入此内容     import android.content.DialogInterface;

我错过了哪一步?

3 个答案:

答案 0 :(得分: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();

这显示了一个简单的AlertDialog。

提示:检查Activity.showDialog(int)和Activity.onCreateDialog(),它们可以让您在使用对话框时更轻松。

答案 1 :(得分:5)

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

Toast.makeText(context, "Hi there!", Toast.LENGTH_SHORT).show();

不要忘记致电show()

答案 2 :(得分:0)

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