2016-12-15 176 views
1

之後雖然學習Android應用中,我創建了這個對話框中使用對話框:創建另一個對話框,對話框的Android

dialogBuilder = new AlertDialog.Builder(this); 
    dialogBuilder.setTitle("Title"); 

    dialogBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

      Toast.makeText(getApplicationContext(), "Next dialog", Toast.LENGTH_LONG).show(); 

     } 
    }); 

    dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show(); 
     } 
    }); 


    AlertDialog dialog = dialogBuilder.create(); 
    dialog.show(); 

如何創建「下一步」被按下後立即出現另一個對話框?我已閱讀this後,但我不明白在那裏回答的代碼。

回答

1

裏面創建積極按鈕的onclick另一個對話框一些這樣的事

dialogBuilder = new AlertDialog.Builder(this); 
dialogBuilder.setTitle("Title"); 

dialogBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 

     Toast.makeText(getApplicationContext(), "Next dialog", Toast.LENGTH_LONG).show(); 

    ShowDialog(); 


    } 
}); 

dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show(); 
    } 
}); 


AlertDialog dialog = dialogBuilder.create(); 
dialog.show(); 

和ShowDialog方法

public void ShowDialog(){ 

dialogBuilder = new AlertDialog.Builder(this); 
dialogBuilder.setTitle("Title"); 

dialogBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 

     Toast.makeText(getApplicationContext(), "Next dialog", Toast.LENGTH_LONG).show(); 

    } 
}); 

dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show(); 
    } 
}); 


AlertDialog dialog = dialogBuilder.create(); 
dialog.show(); 

} 
+0

或者你可以寫裏面positivebutton完整的ShowDialog的onclick – Redman

1

這是我用我的應用程序

Button more = (Button) findViewById(R.id.more); 

more.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View view) { 

     //Intent myIntent = new Intent(view.getContext(), agones.class); 

     //startActivityForResult(myIntent, 0); 


     AlertDialog alertDialog = new 

AlertDialog.Builder(<YourActivityName>this).create(); //Read Update 

     alertDialog.setTitle("hi"); 

     alertDialog.setMessage("this is my app"); 


     alertDialog.setButton("Continue..", new 

DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int which) { 

       // here you can add functions 
      } 
     }); 

     alertDialog.show(); //<-- See This! 
    } 

}); 
0

調用此方法無論你想要的。不需要一次又一次地寫代碼。只需調用方法showConfirmAlertDialog(activity);

public static void showConfirmAlertDialog(Activity mActivity) { 

      AlertDialog.Builder adb = new AlertDialog.Builder(mActivity).setTitle("title").setMessage("message"); 
      adb.setPositiveButton("Next", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { // Confirmed 
        dialog.cancel(); 
       } 
      }); 
      adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { // Rejected 
        dialog.cancel(); 
       } 
      }); 
      adb.setCancelable(false); 
      adb.show(); 
     } 
1

這是您的解決方案,您可以在「下一步」按下後創建另一個對話框。

MainActivity.java

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
         context); 
       alertDialogBuilder.setTitle("First Dialog"); 
       alertDialogBuilder 
         .setMessage("Click yes to Open Second Dialog!") 
         .setCancelable(false) 
         .setPositiveButton("Next", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           //MainActivity.this.finish(); 

           AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
             context); 
           alertDialogBuilder.setTitle("Your Title"); 
           alertDialogBuilder 
             .setMessage("Click yes to exit!") 
             .setCancelable(false) 
             .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog, int id) { 
               //MainActivity.this.finish(); 
              } 
             }) 
             .setNegativeButton("No", new DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog, int id) { 
               // if this button is clicked, just close 
               // the dialog box and do nothing 
               dialog.cancel(); 
              } 
             }); 

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

           // show it 
           alertDialog.show(); 

          } 
         }) 
         .setNegativeButton("No", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           // if this button is clicked, just close 
           // the dialog box and do nothing 
           dialog.cancel(); 
          } 
         }); 

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

       // show it 
       alertDialog.show(); 
      } 
相關問題