2012-12-13 39 views
0

如何在顯示警告對話框之前禁用按鈕,就像在致命對話框"Android error: The application has stopped unexpectedly please try again"上完成一樣。
Android AlertDialog如何在顯示之前禁用按鈕?

我用這樣的例子:

@Override 
    protected Dialog onCreateDialog(int id) { 
    if (id == DIALOG) { 
     Log.d(LOG_TAG, "Create"); 
     AlertDialog.Builder adb = new AlertDialog.Builder(this); 
     adb.setTitle("Title"); 
     adb.setMessage("Message"); 
     adb.setPositiveButton("OK", null); 
     dialog = adb.create(); 

     dialog.setOnShowListener(new OnShowListener() { 
     public void onShow(DialogInterface dialog) { 
      Log.d(LOG_TAG, "Show"); 
     } 
     }); 

     dialog.setOnCancelListener(new OnCancelListener() { 
     public void onCancel(DialogInterface dialog) { 
      Log.d(LOG_TAG, "Cancel"); 
     } 
     }); 

     dialog.setOnDismissListener(new OnDismissListener() { 
     public void onDismiss(DialogInterface dialog) { 
      Log.d(LOG_TAG, "Dismiss"); 
     } 
     }); 
     return dialog; 
    } 
    return super.onCreateDialog(id); 
    } 

    public void onclick(View v) { 
    showDialog(DIALOG); 
    } 

如果我能在dialog.setOnShowListener按鈕,然後得到用戶的可能性就OK按鈕點擊兩次。

+0

我不明白你的問題 – njzk2

+0

如何阻止OK按鈕第二次點擊? – user1879118

+0

要麼A /你不關心或B /刪除第一次點擊聽衆或C /關閉點擊對話框? – njzk2

回答

0

我想你應該在默認情況下禁用它。並使用onShowListener()如下:

dlg.setOnShowListener(new OnShowListener() { 

      @Override 
      public void onShow(DialogInterface dialog) { 
       // TODO Auto-generated method stub 
       //Enable buttons.. 
      } 
     }); 
2
AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
//...All your code to set up the buttons initially 

AlertDialog dialog = alertbox.create(); 
Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); 
if(monsterint > playerint) 
{ 
    button.setEnabled(false); 
} 

使用getButton啓用和禁用

相關問題