Android对话框,按下按钮时保持对话框打开

Bla*_*fix 62 android dialog

当我按下按钮时,我想保持对话框打开.目前正在关闭.

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

builder.setMessage("Are you sure you want to exit?")

   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            MyActivity.this.finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog alert = builder.create();
Run Code Online (Sandbox Code Playgroud)

Kam*_*men 91

是的你可以.你基本上需要:

  1. 使用DialogBu​​ilder创建对话框
  2. show()对话框
  3. 在显示的对话框中找到按钮并覆盖它们 onClickListener

所以,创建一个监听器类:

class CustomListener implements View.OnClickListener {
  private final Dialog dialog;

  public CustomListener(Dialog dialog) {
    this.dialog = dialog;
  }

  @Override
  public void onClick(View v) {

    // Do whatever you want here

    // If you want to close the dialog, uncomment the line below
    //dialog.dismiss();
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在显示对话框时使用:

AlertDialog dialog = dialogBuilder.create();
dialog.show();
Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));
Run Code Online (Sandbox Code Playgroud)

请记住,您需要显示对话框,否则将无法找到该按钮.此外,请务必将DialogInterface.BUTTON_POSITIVE更改为用于添加按钮的任何值.另请注意,在DialogBu​​ilder中添加按钮时,您需要提供onClickListeners- 您无法在其中添加自定义侦听器 - 如果在show()调用后未覆盖侦听器,则对话框仍将被忽略.

  • 我绝对讨厌这个有效.在创建按钮时提供监听器将导致对话框关闭,无论监听器具有什么.如果你等到它被创建(使用"null"作为监听器),然后检索按钮并添加监听器,它就可以完美地工作.谢谢您的帮助. (2认同)

Sha*_*esh 28

感谢Sogger的回答,但是我们必须做一个改变,就是在创建对话框之前,我们应该将AlertDialog设置为正确的按钮(如果有需要的话,还可以设置负按钮),就是这样.

Sogger引用.

这是示例示例......

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Test for preventing dialog close");
        builder.setTitle("Test");

        builder.setPositiveButton("OK", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
    builder.setNegativeButton("Cancel", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });

        final AlertDialog dialog = builder.create();
        dialog.show();
        //Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
              {            
                  @Override
                  public void onClick(View v)
                  {
                      Boolean wantToCloseDialog = false;
                      //Do stuff, possibly set wantToCloseDialog to true then...
                      if(wantToCloseDialog)
                          dialog.dismiss();
                      //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
                  }
              });

        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener()
          {            
              @Override
              public void onClick(View v)
              {
                  Boolean wantToCloseDialog = true;
                  //Do stuff, possibly set wantToCloseDialog to true then...
                  if(wantToCloseDialog)
                      dialog.dismiss();
                  //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
              }
          });
Run Code Online (Sandbox Code Playgroud)


Sog*_*ger 13

我相信@Kamen的答案是正确的,这里是使用匿名类的相同方法的一个例子,所以它在一个代码流中:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test for preventing dialog close");
AlertDialog dialog = builder.create();
dialog.show();
//Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
      {            
          @Override
          public void onClick(View v)
          {
              Boolean wantToCloseDialog = false;
              //Do stuff, possibly set wantToCloseDialog to true then...
              if(wantToCloseDialog)
                  dismiss();
              //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
          }
      });
Run Code Online (Sandbox Code Playgroud)

我在/sf/answers/1093336891/上写了一篇更详细的文章来回答同样的问题,其中还有其他对话框的例子,如DialogFragment和DialogPreference.