0

当我创建两个对话框时,即使另一个对话框的条件为真,也只显示一个..

这是代码

    if (z<=18){   
       showDialog(1);
    } else {
       showDialog(3);
    }
...................................................................................................................................................................
    protected Dialog onCreateDialog(int id)
    {
      if (id== 1)
      {
            AlertDialog.Builder b=new AlertDialog.Builder(this);
            b.setTitle("1st dialog");
            b.setPositiveButton("OK",new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                              // TODO Auto-generated method stub
                        }
                  });
            AlertDialog d=b.create();
            d.show();
      } 
      else 
      {
          AlertDialog.Builder b2=new AlertDialog.Builder(this);
          b2.setTitle("2nd dialog ");         
          b2.setPositiveButton("OK",new DialogInterface.OnClickListener() {

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

          AlertDialog dialoog=b2.create();
          dialoog.show();
      }
      return super.onCreateDialog(id);
    }
4

3 回答 3

0

why don't you initialize both of your AlertDialogs, and depending of the situation you just show one:

defining the dialogs:

    //infodialog
    infodialog = new AlertDialog.Builder(this).create();  
    infodialog.setTitle("INFO");  
    infodialog.setMessage(getApplicationContext().getString(R.string.info));  
    infodialog.setButton("OK", new DialogInterface.OnClickListener() {  
      public void onClick(DialogInterface dialog, int which) {  
        return;  }
    });
  //disclaimerdialog
    disclaimerdialog = new AlertDialog.Builder(this).create();  
    disclaimerdialog.setTitle("Disclaimer");  
    disclaimerdialog.setMessage(getApplicationContext().getString(R.string.disclaimer));  
    disclaimerdialog.setButton("OK", new DialogInterface.OnClickListener() {  
      public void onClick(DialogInterface dialog, int which) {  
        return;  }
    });

and picking one (i used a optionmenu and switch, you can use any if-function with your dialog.show ofcourse):

public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()){

                case R.id.info:
                    infodialog.show();
                    return true;
                case R.id.disclaimer:
                    disclaimerdialog.show();
                    return true;
于 2012-06-20T18:40:57.060 回答
0

如果你在 if - else 中有它们,这两个条件怎么可能是真的?

于 2012-06-20T18:36:55.420 回答
0

这两种情况会true怎样?您正在使用if else彼此专有的条件检查,一次只有一个可以工作。因此只显示一个对话框。

于 2012-06-20T18:37:17.597 回答