2011-09-28 80 views
4

我想一個alertdialog.But無法看到內添加alertdialog第二alertdialog..please幫我 這裏顯示我的代碼AlertDialog內alertdialog安卓

AlertDialog alertDialog = new AlertDialog.Builder(myclass.this).create(); 
alertDialog.setTitle("First alert"); 
alertDialog.setMessage("first alert press"); 
alertDialog.setButton("ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
     // here you can add functions 
     dialog.cancel(); 

     AlertDialog alertDialog1 = new AlertDialog.Builder(myclass.this).create(); 
     alertDialog1.setTitle("second alert dialog"); 
     alertDialog1.setMessage("second alert dialog details"); 
     alertDialog1.setButton("Scan Another", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
}}); } 
+0

這是不良的用戶界面行爲。使用吐司。 – Reno

+0

但我需要執行兩個不同的操作 – nikhilkilivayil

+0

這是設計,以便吸引類型的UI行爲不鼓勵。爲什麼不擴展第一個警報對話框以包含您的操作?或者創建第一個警報對話框,一個活動。 – Reno

回答

3

它會是一個遲到答案,但你可以創建內部onClickListener一個AlertDialog就像這樣:

public void onClick(DialogInterface dialog, int which) { 
           // TODO Auto-generated method stub 
           if (options[which] == "Manage") { 

           } 
           else { 
            dialog.dismiss(); 
            final AlertDialog alert; 

            AlertDialog.Builder dialog2 = new AlertDialog.Builder(CategoryPage.this); 
            alert = dialog2.create(); 
            alert.setTitle("Delete " + title + "?"); 
            alert.setMessage("Are you sure you want to delete this category?"); 

            alert.setButton("Yes", new DialogInterface.OnClickListener() { 

             public void onClick(DialogInterface dialog, int which) { 
              // TODO Auto-generated method stub 
              Toast.makeText(CategoryPage.this, "YESS", Toast.LENGTH_LONG).show(); 
             } 
            }); 

            alert.setButton2("No", new DialogInterface.OnClickListener() { 

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

            alert.show(); 
           } 
          } 
         }); 
1

我發現的方式可能是會有幫助的人所以這就是爲什麼我分享:

//2nd Alert Dialog 
       AlertDialog.Builder alertDialogBuilderSuccess = new AlertDialog.Builder(
         context); 
       alertDialogBuilderSuccess.setTitle("TopUp Success"); 
       // set dialog message 
       alertDialogBuilderSuccess 
         .setMessage(
           "You voucher is printed, please go to the cashier.") 
         .setCancelable(false) 
         .setIcon(R.drawable.ic_launcher2) 
         .setPositiveButton("Confirm", 
           new DialogInterface.OnClickListener() { 
            public void onClick(
              DialogInterface dialog, int id) { 

             finish(); 
            } 
           }); 

       // create alert dialog 
       final AlertDialog alertDialogSuccess = alertDialogBuilderSuccess.create(); 








       ////////////////////////////////// 
       //1st Alert 
       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
         context); 
       alertDialogBuilder.setTitle("TopUp Request"); 
       // set dialog message 
       alertDialogBuilder 
         .setMessage(
           "Please confirm: " + vendor_name + ", " 
             + tvLoadAmount.getText()) 
         .setCancelable(false) 
         .setIcon(R.drawable.ic_launcher2) 
         .setPositiveButton("Confirm", 
           new DialogInterface.OnClickListener() { 
            public void onClick(
              DialogInterface dialog, int id) { 

             //calling the second alert when it user press the confirm button 
             alertDialogSuccess.show(); 
            } 
           }) 
         .setNegativeButton("Cancel", 
           new DialogInterface.OnClickListener() { 
            public void onClick(
              DialogInterface dialog, int id) { 
             dialog.cancel(); 
            } 
           }); 

       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 

       // show it 
       alertDialog.show(); 
0

AlertDialogs不應該讓另一個AlertDialog打開。如果它真的是你想要的,那麼將你的主AlertDialog改爲Dialog。通過這種方式,您可以手動添加所需的按鈕和功能,以管理次級AlertDialog