简体   繁体   English

如何在android中提示用户使用类似Toast的消息?

[英]how do you prompt the user with a toast-like message in android?

I want the user to confirm an action by showing them a dialogue with a message and "yes" or "no" buttons. 我希望用户通过向消息和“是”或“否”按钮显示对话来确认操作。 How can I make that appear, and perform actions based on the button they pick? 如何显示,并根据他们选择的按钮执行操作?

Thanks, AlertDialog looks like what I'm looking for. 谢谢,AlertDialog看起来像我正在寻找的。 But, there's an error where it says "AlertDialog.Builder(this);" 但是,它出现了"AlertDialog.Builder(this);"的错误"AlertDialog.Builder(this);" that tells me, "The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined" 告诉我, "The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined" -

As shown here : 如图所示在这里

private static final int DIALOG = 1;

to show the dialog call 显示对话框调用

showDialog(DIALOG);

override onCreateDialog, check with a switch for the dialog ID and insert 覆盖onCreateDialog,用交换机检查对话框ID并插入

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure about this?")
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            // whatever if YES
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            // whetever if NO
       }
   });
AlertDialog alert = builder.create();

What you're looking for is an AlertDialog . 您正在寻找的是AlertDialog Using the example here , you can easily create a Yes/No dialog that will look something along the lines of: 使用此处的示例,您可以轻松创建“是/否”对话框,该对话框将显示以下内容:

在此输入图像描述

You create a new AlertDialog.Builder , pass it some parameters, finally invoke its create() method and assign the return value to an AlertDialog object to hold a reference to it. 您创建一个新的AlertDialog.Builder ,传递一些参数,最后调用其create()方法并将返回值分配给AlertDialog对象以保存对它的引用。

AlertDialog.Builder adb = new AlertDialog.Builder(this);
        adb.setTitle("Question");
        adb.setMessage(Html.fromHtml("Visit Stackoverflow?"));
        adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Action for YES
                startActivity(
                         new Intent(
                                Intent.ACTION_VIEW,
                                Uri.parse("http://www.stackoverflow.com")));
                return;
            }
        });

        adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Action for NO
                return;
            }
        });

AlertDialog myDialog = adb.create();

it's simple as below 它很简单如下

new AlertDialog.Builder(this)
            .setTitle("Info")
            .setMessage("hello")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    // Some stuff to do when ok got clicked                 
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    // Some stuff to do when ok got clicked
                }
            })
            .show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM