在单选警报对话框中启用/禁用正按钮

时间:2014-01-10 11:31:25

标签: android android-alertdialog

如果你可以帮助我,我有一个选择警告对话框有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;

非常感谢您的帮助。 谢谢

3 个答案:

答案 0 :(得分:4)

终于有了工作。感谢Piyush和Mystic Magic的帮助。在这里发布代码可能对其他人有帮助。 全局初始化buttonPositiveInvolvement Button buttonPositiveInvolvement;

总结:选择项目时,在单选AlertDialog中启用/禁用“正”按钮。 (最初禁用NEXT按钮,当选择一个项目时,它会启用NEXT按钮)

//全局 Button 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;

答案 1 :(得分:0)

您需要启用onClick中的按钮,如下所示:

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

这样就可以在选择任何选项时启用按钮。

希望这有帮助。

答案 2 :(得分: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
        }
    }
}

我已经搜索了更多.....然后我将根据您的要求给出一些逻辑......我认为这段代码可以根据您的要求完美运行。