【问题标题】:Showing the same AlertDialog again再次显示相同的 AlertDialog
【发布时间】:2017-02-09 09:12:22
【问题描述】:

我正在测试 AlertDialog 的行为以集成到更大的组件中。我无法再次显示相同的对话框。下面是测试代码:

public class MainActivity extends AppCompatActivity {

    private AlertDialog alertDialogACCreationRetry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alertDialogACCreationRetry = new AlertDialog.Builder(this)
                .setTitle("Account creation failed")
                .setMessage("There was a problem connecting to the Network. Check your connection.")
                .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).create();
        alertDialogACCreationRetry.show();

        alertDialogACCreationRetry.show();
    }
}

我尝试将alertDialogACCreationRetry.show(); 放在重试按钮中,但它仍然不会显示。我也尝试将alertDialogACCreationRetry.dismiss(); 放在重试按钮内,然后在外面调用alertDialogACCreationRetry.show();,它仍然没有显示。更可怕的是,如果不应该允许,它不会给我一个例外来重新显示它。

所以,我的问题是这样的:每次按下按钮后(自动)解除对话框后,我是否都必须创建一个新对话框?

【问题讨论】:

  • 您可以尝试使用 Dialog。
  • 是的,你会再创建一个新的对话实例@pulp_fiction
  • @АксеновВладимир :我可以覆盖正面按钮行为,但是当我调用 alertDialogACCreationRetry.show();稍后,即使没有按钮覆盖,它也应该重新显示对话框。

标签: android android-alertdialog


【解决方案1】:
public void showAlertDialog() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Time");
    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i) {
            dialog.cancel();
            // call function show alert dialog again
            showAlertDialog();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    final AlertDialog alert = builder.create();
    alert.show();
}

【讨论】:

    【解决方案2】:

    可能发生的情况是您在对话框被关闭之前调用 .show() ,因此它是无操作的。当您从重试按钮侦听器返回时,对话框将被关闭。

    PS 这似乎与我的建议一致:https://stackoverflow.com/a/12110191/3286819

    要完成这项工作,您应该在网络代码真正重试操作后再次调用 show,可能在后台线程或类似的东西中。

    最后,另一方面,简单地再次创建对话框可能更有意义,因为在某些情况下保持相同的对话框可能会造成泄漏。

    【讨论】:

    • 排除网络延迟问题,问题是,我尝试在再次调用 show() 之前调用dismiss() 而不仅仅是一次,我写了 alertDialogACCreationRetry.show() 7-8 次系列,它仍然没有显示
    • 问题是真正发生的事情是对话框本身在您从侦听器返回后立即调用 .dismiss() 。一个解决方案是稍后调用 .show()(例如使用 .postDelayed(),如下所示:Handler handlerTimer = new Handler(); handlerTimer.postDelayed(new Runnable(){ public void run() { /* .show() */ }}, 100); 无论如何请注意这是一个 hack。
    【解决方案3】:

    你可以这样做:

    public class MainActivity extends AppCompatActivity {
    
    private AlertDialog alertDialogACCreationRetry;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        alertDialogACCreationRetry = new AlertDialog.Builder(this)
                .setTitle("Account creation failed")
                .setMessage("There was a problem connecting to the Network. Check your connection.")
                .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).create();
    
        alertDialogACCreationRetry.show();
    
        alertDialogACCreationRetry.getButton(AlertDialog.BUTTON_POSITIVE).
            setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean yourLogicGoFine = false;
                if (yourLogicGoFine){
                    alertDialogACCreationRetry.dismiss();
                }else{
                    Toast.makeText(finalizarActivity, "Something was wrong",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
      }
    }
    

    祝你好运!

    【讨论】:

      【解决方案4】:

      您无需重新创建alertDialogACCreationRetry。您可以重复使用它,但不能一次显示多个具有相同对象的警报对话框。 show() 方法在内部检查当前警报对话框是否已经显示。如果它正在显示,那么它将返回什么都不做,将显示新的对话框。

      因此,请确保在对话框中调用 show() 之前调用 dismiss()

      这是我尝试过的

      @Nullable @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
      getActivity().getSupportFragmentManager();
      
      alertDialogACCreationRetry = new AlertDialog.Builder(getActivity())
          .setTitle("Account creation failed")
          .setMessage("There was a problem connecting to the Network. Check your connection.")
          .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
      
            }
          })
          .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
          }).create();
      alertDialogACCreationRetry.show();
      
      View view = inflater.inflate(R.layout.frag1, container, false);
      TextView viewById = (TextView) view.findViewById(R.id.frag1_text);
      viewById.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          alertDialogACCreationRetry.show();
        }
      });
      return view;
      }
      

      在上面的alertDialogACCreationRetry.show() 内部调用onClick() 将显示对话框,如果没有对话框已经显示,否则它什么也不做。

      【讨论】:

      • 我在再次调用 show() 之前确实关闭了它,但它不起作用(我已经提到过这个问题)
      • 您也可以添加该代码。您应该确定首先调用的是 show() 还是 dismiss()
      【解决方案5】:

      正如其他人所说,调用 show() 不会使对话框出现的原因是因为它还没有被解除。该方法通过设置对话框的可见性来工作,而不是添加另一个对话框,因此您绝对无法获得同一个对话框的 2 层。

      您可以添加一个按钮来触发显示对话框,以确保对话框完全关闭。像这样:

      findViewById(R.id.show_button).setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
             alertDialogACCreationRetry.show();
          }
      });
      

      关闭对话框后单击按钮。它肯定会重新出现。

      【讨论】:

      • 查看接受的解决方案。即使没有添加按钮覆盖,它也可以工作。这是一个优雅的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      相关资源
      最近更新 更多