2014-12-27 26 views
0

我正在學習Android,我在創建對話框時遇到了一個問題。我想單擊負面按鈕System.exit(0)。但我不知道該怎麼做。請幫幫我。下面是我的代碼:想法需要在android AlertDialogue框

new AlertDialog.Builder(this) 
        .setTitle("Check Below..") 
        .setMessage("No Internet Connection") 
        .setCancelable(true) 
        .setPositiveButton("Go Back", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 

          finish(); 

         } 
        }) 
        .setNegativeButton("Exit", null).show(); 


} 
+1

什麼是錯誤/問題? – Rohit5k2 2014-12-27 20:27:16

+0

@ Rohit5k2我不知道該怎麼做 – 2014-12-27 20:28:43

+1

你到底在想什麼?你想在正面的按鈕點擊和負面的按鈕點擊什麼?我還記得幾天前請求你不要使用'System.exit(0)'。這是一個壞主意。 – Rohit5k2 2014-12-27 20:29:26

回答

1

做這樣的事情

new AlertDialog.Builder(this) 
       .setTitle("Check Below..") 
       .setMessage("No Internet Connection") 
       .setCancelable(true) 
       .setPositiveButton("Go Back", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

         finish(); 

        } 
       }) 
       .setNegativeButton("Exit", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

         System.exit(0); 

        } 
       }).show(); 
+0

非常感謝,我有一個疑問。爲什麼我們需要在負面按鈕中添加.show(),但不在正面按鈕中? plz – 2014-12-27 20:38:21

+0

您不在追加按鈕中附加'show()'。你將它追加到'Builder'中。看到它繼續使用點運算符。 – Rohit5k2 2014-12-27 20:39:39

+0

確定有dat ..非常感謝... – 2014-12-27 20:40:29

4

如果你想殺死活性負單擊按鈕時,您只需寫:

ClassName.this.finish();