2015-10-12 117 views
0

我需要幫助!單擊按鈕後請勿關閉AlertDialog

我正在開發一個android應用程序,我遇到了一個問題。 我有一個AlertDialog其中包含兩個按鈕(正面和負面)。當點擊某個按鈕時,會運行某個代碼,然後關閉該對話框。

dialog.setNegativeButton("button name", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // some code 
     } 
    }); 

但這不是我想要的。當用戶點擊否定按鈕時,我想要運行一些代碼,然後不應該關閉對話框。

dialog.setNegativeButton("button name", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // some code 
      // code to prevent the dialog from being closed ? 
     } 
    }); 

有什麼我可以做的,以防止點擊正面或負面按鈕時關閉對話框?

我嘗試使用此代碼:

dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false); 

但是因爲現在用戶不能點擊按鈕不起作用。

順便說一句,我開發的16

感謝您的幫助最小SDK版本!

+0

創建您自己的自定義對話框 –

+0

您的一些代碼是否還包含'dialog.dismiss()'? –

+0

可能的重複http://stackoverflow.com/questions/4016313/how-to-keep-an-alertdialog-open-after-button-onclick-is-fired – stinepike

回答

0

如果在的onClick代碼中包含dialog.dismiss()則僅對話框會得到關閉

0
String Strmessage="message"; 
      final AlertDialog.Builder alt_bld = new AlertDialog.Builder(SplashActivity.this); 
      alt_bld.setMessage(Strmessage); 
      alt_bld.setCancelable(true); 
      alt_bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        dialog.dismiss(); 
       } 
      }) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.dismiss(); 
       } 
      }); 
      AlertDialog alert = alt_bld.create();   
      alt_bld.setMessage(Strmessage); 
      alert.show(); 

     } 
0

感謝您的建議。我做了一個自定義對話框,現在它就像我想要的那樣工作。