2013-04-22 56 views
0

我的Android應用程序有一個正面按鈕和3個單選按鈕的對話框。
2/3是錯誤的答案,1是正確的答案。
當用戶選擇錯誤的答案,然後按possitiveButton爲OK,
我不想關閉該對話框並顯示敬酒消息說
你的選擇是不正確。
然後用戶可以重新選擇另一個答案,然後按確定,
如果答案是正確的,我想關閉對話框。
我想知道在按下possitiveButton時如何不關閉對話框。
當possitiveButton按下時不要關閉對話框

謝謝!

+0

答案是http://stackoverflow.com/questions/10669948/alertdialog- is-this-a-bug – 2013-04-22 15:54:35

回答

0

您必須重寫positiveButton的Click方法。

AlertDialog.Builder db = new AlertDialog.Builder(MyActivity.this); 
db.setPositiveButton("OK", new 
    DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 

     if (rb1.isChecked() || rb2.isChecked()){ //here make the toast} 
      } 
     }); 
     else { //dismiss here} 
AlertDialog dialog = db.show(); 

其中RB1和RB2是錯誤的單選按鈕

+0

謝謝你的代碼。但是我的問題是當我按下PositiveButton時,即使我不寫dismiss(),對話也會被解除。 – MaryJJ 2013-04-23 01:46:22

+0

所以如果你現在檢查rb1(錯誤的答案)並點擊確定,對話框會被解僱?在這種情況下,請嘗試更新的代碼。 – 2013-04-23 09:19:54

+0

我發現setPositiveButton的onClickListener必須關閉對話框。因此,我禁用它並將setOnClickListener設置爲getButton(DialogInterface.BUTTON_POSITIVE)。這工作正常。謝謝你們所有的! – MaryJJ 2013-04-25 05:08:09

1

我將給你一個例子

AlertDialog.builder builder = new Builder(this); 
builder.setPositiveButxxxxx(xxxxxx) 
{ 
    onClick(AlertDialog alert) 
    { 
      //if you want to dissmiss here, 
      alert.dismiss(); 
      //else 
      //do nothing 
    } 
} 
相關問題