如何使用OnDismiss才能使用对话框输入?

How to use OnDismiss in order to use dialog box input?

我有一个对话框提示用户,用户输入将在代码的下一步中使用。我正在尝试使用 OnDismiss 使其余代码接受用户输入。以下是代码片段:

AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Title");
    alert.setMessage("Message");

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {

        input = 3;
      }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
          input = 1;
      }
    });

    alert.show();

    }

public void OnDismiss(DialogInterface dialogInterface)
{
Toast.makeText(this, "Input: " + input, Toast.LENGTH_LONG)
.show();

}

但是,当我按下对话框按钮时,对话框消失了,但是吐司没有出现。请在这里提出我做错了什么。

首先将 OnDismissListener 设置为您的 AlertDialog 然后在按下对话框按钮时调用 dismiss(),代码如下:

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Title");
    alert.setMessage("Message");

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        input = 3;
        dialog.dismiss();
      }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
          input = 1;
          dialog.dismiss();
      }
    });

   alert.setOnDismissListener(new OnDismissListener(){
       public void OnDismiss(DialogInterface dialogInterface)
         {
          Toast.makeText(this, "Input: " + input, Toast.LENGTH_LONG)
         .show();
          }
   });

    alert.show();

你必须使用 dialog.dismiss();

  alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {

    input = 3;
   dialog.dismiss();
  }
});

  alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
      input = 1;
  dialog.dismiss();
  }
});