在AlertDialog中调用同一个AlertDialog

发布于 2024-12-10 02:13:59 字数 1290 浏览 0 评论 0原文

我有一个 AlertDialog 提示用户是否要发送数据。我正在做的是检查是否有互联网连接,如果没有,我将再次显示该对话框。 该对话框显示,但当我单击“是”时,连接断开时它不会显示相同的对话框。

public void sendData(){
    boolean connected = checkConnectivity(getApplicationContext());
    //connected is false, but dialog doesnt show the second time.

           if(connected==false){
               //show dialog
               showDialog(0);
           }else{
               //connected, send data
           }
        }

@Override
protected Dialog onCreateDialog( int id ) 
{

        return 
    new AlertDialog.Builder( this )
        .setTitle( "Send data?" )
        .setPositiveButton( "Yes", new DialogButtonClickHandler() )
        .setNegativeButton( "No", new DialogButtonClickHandler() )
        .create();

}

public class DialogButtonClickHandler implements DialogInterface.OnClickListener
{
    public void onClick( DialogInterface dialog, int clicked )
    {

        switch( clicked )
        {
            case DialogInterface.BUTTON_POSITIVE:
                //Problem occurs here. sendData() gets called but dialog not displayed the second time
                            sendData();
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                return;

        }
    }
}

有人可以帮忙吗?

I have an AlertDialog that prompts user if they want to send data. What I am doing is that I check if there is internet connection, if not I will display the dialog again.
The dialog displays but when I click 'yes', it does not display the same dialog when the connection is down.

public void sendData(){
    boolean connected = checkConnectivity(getApplicationContext());
    //connected is false, but dialog doesnt show the second time.

           if(connected==false){
               //show dialog
               showDialog(0);
           }else{
               //connected, send data
           }
        }

@Override
protected Dialog onCreateDialog( int id ) 
{

        return 
    new AlertDialog.Builder( this )
        .setTitle( "Send data?" )
        .setPositiveButton( "Yes", new DialogButtonClickHandler() )
        .setNegativeButton( "No", new DialogButtonClickHandler() )
        .create();

}

public class DialogButtonClickHandler implements DialogInterface.OnClickListener
{
    public void onClick( DialogInterface dialog, int clicked )
    {

        switch( clicked )
        {
            case DialogInterface.BUTTON_POSITIVE:
                //Problem occurs here. sendData() gets called but dialog not displayed the second time
                            sendData();
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                return;

        }
    }
}

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2024-12-17 02:13:59

隔了这么久终于找到答案了!在 sendData() 方法中,您需要重建对话框,而不是调用 showDialog()

public void sendData(){
boolean connected = checkConnectivity(getApplicationContext());
//connected is false, but dialog doesnt show the second time.

       if(connected==false){
           //rebuild and show dialog
           AlertDialog newDialog = new AlertDialog.Builder( this )
          .setTitle( "Send data?" )
          .setPositiveButton( "Yes", new DialogButtonClickHandler() )
          .setNegativeButton( "No", new DialogButtonClickHandler() )
          .create();
          newDialog.show();


       }else{
           //connected, send data
       }
    }

Figured out the answer after so long! In the sendData() method, instead of calling showDialog(), you need to rebuild the dialog

public void sendData(){
boolean connected = checkConnectivity(getApplicationContext());
//connected is false, but dialog doesnt show the second time.

       if(connected==false){
           //rebuild and show dialog
           AlertDialog newDialog = new AlertDialog.Builder( this )
          .setTitle( "Send data?" )
          .setPositiveButton( "Yes", new DialogButtonClickHandler() )
          .setNegativeButton( "No", new DialogButtonClickHandler() )
          .create();
          newDialog.show();


       }else{
           //connected, send data
       }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文