等待Android中的对话框响应

时间:2018-03-19 03:58:35

标签: java android android-dialog

我正在将平台从C#转换为android。

我被困在如何等待Dialog响应, 确定将采取哪种行动。 (它总会绕过决定。)

我曾尝试使用处理程序,但它不起作用。

感谢任何帮助。

这是我的代码,alertDialog不会等待对话框响应完成。

public boolean CheckWriteCriteria()
{

        new AlertDialog.Builder(Labelling.this)
                .setTitle("Test 1")
                .setMessage("Question_1")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(this, "YES button click ", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(this, "No button click ", Toast.LENGTH_SHORT).show();
                    }
                }).show();


        new AlertDialog.Builder(Labelling.this)
                .setMessage("Question_2")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        Toast.makeText(this, "YES button click ", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(this, "No button click ", Toast.LENGTH_SHORT).show();
                    }
                }).show();


    return true;
}




 public void Testing()
{

    if(CheckWriteCriteria())
    {
        Toast.makeText(this, "Testing is before AlertDialog.", Toast.LENGTH_SHORT).show();
    }

}

1 个答案:

答案 0 :(得分:1)

Android支持AlertDialog。让我们看看

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            MainActivity.this);

    // set title
    alertDialogBuilder.setTitle("AlertDialog Title");

    // set dialog message
    alertDialogBuilder
            .setMessage("Some Alert Dialog message.")
            .setCancelable(false)
            .setPositiveButton("OK",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                            Toast.makeText(this, "OK button click ", Toast.LENGTH_SHORT).show();

                }
            })
            .setNegativeButton("CANCEL",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                           Toast.makeText(this, "CANCEL button click ", Toast.LENGTH_SHORT).show();

                    dialog.cancel();
                }
            });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();

这里只有Ok和Cancel动作,如果你想要更多,只需定义自定义对话框。