tangguo

如何防止单击按钮时对话框关闭

java

我有一个EditText用于输入的对话框。当我单击对话框上的“是”按钮时,它将验证输入,然后关闭对话框。但是,如果输入错误,我希望保留在同一对话框中。每次无论输入什么,当我单击“否”按钮时,都应自动关闭对话框。如何禁用此功能?顺便说一句,我在对话框上的按钮上使用了PositiveButton和NegativeButton。


阅读 371

收藏
2020-12-07

共2个答案

一尘不染

编辑:这仅在API 8+上有效,如某些注释所述。

这是一个较晚的答案,但是您可以在AlertDialog中添加onShowListener,然后在其中可以覆盖按钮的onClickListener

final AlertDialog dialog = new AlertDialog.Builder(context)
        .setView(v)
        .setTitle(R.string.my_title)
        .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
        .setNegativeButton(android.R.string.cancel, null)
        .create();

dialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialogInterface) {

        Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Do something

                //Dismiss once everything is OK.
                dialog.dismiss();
            }
        });
    }
});
dialog.show();
2020-12-07
一尘不染

这是适用于所有类型对话框的一些解决方案,包括适用于所有API级别的AlertDialog.Builder解决方案(在API 8以下工作,此处其他答案则不行)。有使用AlertDialog.Builder,DialogFragmentDialogPreference的AlertDialogs解决方案。

下面的代码示例显示如何覆盖默认的通用按钮处理程序,以及如何防止这些不同形式的对话框关闭对话框。所有示例都说明了如何防止肯定按钮关闭对话框。

注意:对于那些想要了解更多详细信息的人,在示例之后,将介绍基本android类的对话框关闭操作方式以及为什么选择以下方法的说明。

AlertDialog.Builder-在show()之后立即更改默认按钮处理程序
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test for preventing dialog close");
builder.setPositiveButton("Test", 
        new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                //Do nothing here because we override this button later to change the close behaviour. 
                //However, we still need this because on older versions of Android unless we 
                //pass a handler the button doesn't get instantiated
            }
        });
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.
          }
      });

DialogFragment-重写onResume()

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Test for preventing dialog close");
    builder.setPositiveButton("Test", 
        new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                //Do nothing here because we override this button later to change the close behaviour. 
                //However, we still need this because on older versions of Android unless we 
                //pass a handler the button doesn't get instantiated
            }
        });
    return builder.create();
}

//onStart() is where dialog.show() is actually called on 
//the underlying dialog, so we have to do it there or 
//later in the lifecycle.
//Doing it in onResume() makes sure that even if there is a config change 
//environment that skips onStart then the dialog will still be functioning
//properly after a rotation.
@Override
public void onResume()
{
    super.onResume();    
    final AlertDialog d = (AlertDialog)getDialog();
    if(d != null)
    {
        Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Boolean wantToCloseDialog = false;
                        //Do stuff, possibly set wantToCloseDialog to true then...
                        if(wantToCloseDialog)
                            d.dismiss();
                        //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
                    }
                });
    }
}

DialogPreference-覆盖showDialog()


@Override
protected void onPrepareDialogBuilder(Builder builder)
{
    super.onPrepareDialogBuilder(builder);
    builder.setPositiveButton("Test", this);   //Set the button here so it gets created
}

@Override
protected void showDialog(Bundle state)
{       
    super.showDialog(state);    //Call show on default first so we can override the handlers

    final AlertDialog d = (AlertDialog) getDialog();
    d.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)
                        d.dismiss();
                    //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
                }
            });
}

方法说明:

通过查看Android源代码,AlertDialog默认实现是通过向OnCreate()中的所有实际按钮注册一个通用按钮处理程序来工作的。单击按钮后,通用按钮处理程序会将click事件转发到您在setButton()中传递的任何处理程序,然后调用将关闭对话框。

如果希望防止在按下这些按钮之一时关闭对话框,则必须为按钮的实际视图替换通用按钮处理程序。因为它是在OnCreate()中分配的,所以必须在调用默认的OnCreate()实现后替换它。在show()方法的过程中调用OnCreate。您可以创建一个自定义Dialog类并重写OnCreate()来调用super.OnCreate()然后重写按钮处理程序,但是如果您创建一个自定义对话框,您将无法免费获得Builder,在这种情况下,重点是什么?

因此,在使用对话框的设计方式但要控制何时关闭对话框时,一种方法是先调用dialog.Show(),然后使用dialog.getButton()获取对按钮的引用以覆盖单击处理程序。另一种方法是使用setOnShowListener()并实现查找按钮视图并替换OnShowListener中的处理程序。两者之间的功能差异几乎是“零”,这取决于最初创建对话框实例的线程。查看源代码,通过显示在创建该对话框的线程上运行的处理程序的消息调用onShowListener。因此,由于您的OnShowListener是由消息队列中发布的消息调用的,因此从技术上讲,在显示完成后,调用您的侦听器可能会延迟一段时间。

因此,我认为最安全的方法是第一种:调用show.Dialog(),然后立即在同一执行路径中替换按钮处理程序。由于调用show()的代码将在主GUI线程上运行,因此,跟在show()之后的任何代码都将在该线程上的任何其他代码之前执行,而OnShowListener方法的运行时间受制于消息队列。

2020-12-07