2014-01-10 66 views
1

如果您可以幫助我解決這個問題,我有一個單選警報對話框有3個選項。我最初禁用了NEXT按鈕(沒有選擇任何選項)。當用戶選擇任何選項時,我想啓用NEXT按鈕。這是我試過的代碼。在單選警報對話框中啓用/禁用肯定按鈕

int involvementInIncident; 

case DIALOG_ADD_A_PERSON_INVOLVEMENT_ONE: 

     builder.setTitle("Involvement in this Incident"); 
     builder.setSingleChoiceItems(incidentInvolvement, -1, 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         involvementInIncident = which;       
         //involvementInIncident = ((AlertDialog)dialog).getListView().getCheckedItemPosition(); 
         Toast.makeText(getApplicationContext(),""+ involvementInIncident, Toast.LENGTH_SHORT).show(); 


        } 
       });   
     builder.setPositiveButton("Next", 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) {      
         //Toast.makeText(getApplicationContext(),""+ involvementInIncident, Toast.LENGTH_SHORT).show(); 
         showDialog(DIALOG_ADD_A_PERSON_PERSON_TYPE_TWO); 

        } 

       }); 
     builder.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 


        } 
       });  
     AlertDialog alertInvolvement = builder.create(); 
     alertInvolvement.show();    
     final Button buttonPositiveInvolvement = alertInvolvement.getButton(AlertDialog.BUTTON_POSITIVE); 
     buttonPositiveInvolvement.setEnabled(false); 

     if(involvementInIncident == 0 || involvementInIncident == 1 || involvementInIncident==2){ 
      buttonPositiveInvolvement.setEnabled(true); 
      //Toast.makeText(getApplicationContext(),"person responsiblee", Toast.LENGTH_SHORT).show(); 

     } 
     else 
     { 
      buttonPositiveInvolvement.setEnabled(false); 
     } 

     return dialog; 

您的幫助非常感謝。 謝謝

+0

WHU你不能創造我們的定製dialog.so你可以輕鬆地處理所有這些事情 – dipali

+0

你可以用這一個經歷,因爲這個環節也解決了礦山的同樣的問題。它根據需要顯示帶三個按鈕的警報對話框。 http://www.learn2crack.com/2013/11/android-alertdialog-example.html – Poison

回答

0

您需要啓用onClick這樣的按鈕:

@Override 
public void onClick(DialogInterface dialog, int which) { 
    //your other code for toast 
    buttonPositiveInvolvement.setEnabled(true); 
} 

所以,這將使任何選擇的選擇按鈕。

希望這會有所幫助。

+0

我也試過。它說「無法解決」,因爲我已經在「show()」方法之後初始化buttonPositiveInvolvement。 – BRDroid

+0

你有沒有聲明** buttonPositiveInvolvement **全局? – Piyush

+0

在show method之前初始化它。 @ user3046669 –

0
public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     calldialog(0); 
    } 

    int call = 0; 

    public void calldialog(final int call1) { 
     call = call1; 
     final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setIcon(android.R.drawable.ic_dialog_info); 
     builder.setTitle("Alert dialog title"); 
     builder.setMessage("Dialog message"); 
     builder.setCancelable(false); 

     builder.setPositiveButton("cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface arg0, int arg1) { 
         builder.setCancelable(false); 
         call = 1; 
         calldialog(call); 
        } 
       }); 
     builder.setNegativeButton("ok", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       call = 1; 
       calldialog(call); 
      } 
     }); 
     builder.setNeutralButton("next", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       call = 0; 
       calldialog(call); 
      } 
     }); 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 

     // After calling show method, you need to check your condition and 
     // enable/ disable buttons of dialog 
     if (call == 0) { 
      dialog.getButton(Dialog.BUTTON_NEUTRAL).setEnabled(false); // BUTTON1 
     } else { 
      dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false); // BUTTON1 
      dialog.getButton(Dialog.BUTTON_NEGATIVE).setEnabled(false); // BUTTON1 
     } 
    } 
} 

我有搜索更多.....然後我會給予一定的邏輯,按您的requriment..i覺得這個代碼工作完美的按照您的要求。

+0

嗨,我的要求是我有一個singleChoiceItem有3個選擇,最初NEXT按鈕被禁用,它在我的程序中正常工作,但是當選擇一個項目時,我想啓用NEXT按鈕。 我在這部分掙扎。 – BRDroid

4

終於搞定了。感謝Piyush和Mystic Magic的幫助。在這裏發佈代碼可能會幫助其他人。 全局初始化buttonPositiveInvolvement Button buttonPositiveInvolvement;

總結:選擇項目時,在單選AlertDialog中啓用/禁用「正」按鈕。 (最初NEXT按鈕被禁用,當一個項目被選中時它啓用了NEXT按鈕)

//全局 按鈕buttonPositiveInvolvement;

情況下DIALOG_ADD_A_PERSON_INVOLVEMENT_ONE:

 builder.setTitle("Involvement in this Incident"); 
     builder.setSingleChoiceItems(incidentInvolvement, -1, 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) {            
         //to enable the the NEXT button 
         buttonPositiveInvolvement.setEnabled(true); 

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

         showDialog(DIALOG_ADD_A_PERSON_PERSON_TYPE_TWO); 

        } 

       }); 
     builder.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 


        } 
       });    
     //This sequence needs to be followed to get it working or else it might screw up 
     AlertDialog alertInvolvement = builder.create();    
     alertInvolvement.show(); 
     buttonPositiveInvolvement = alertInvolvement.getButton(AlertDialog.BUTTON_POSITIVE);    
     buttonPositiveInvolvement.setEnabled(false); 

     return dialog; 
相關問題