在单选警告对话框中启用/禁用肯定按钮

3

如果您能帮我,我有一个包含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;

非常感谢您的帮助。

谢谢。


你可以尝试这个链接,因为它也解决了我的同样的问题。它展示了一个带有三个按钮的警告对话框,正如要求的那样。http://www.learn2crack.com/2013/11/android-alertdialog-example.html - Poison
为什么你不创建我们自己的对话框,这样你就可以轻松处理所有这些事情。 - dipali
3个回答

6
终于搞定了。感谢Piyush和Mystic Magic的帮助。我在这里发布代码,或许可以帮助其他人。 全局初始化buttonPositiveInvolvement Button buttonPositiveInvolvement;
总结一下:当选择一个项目时,在单选AlertDialog中启用/禁用Positive按钮。(最初,NEXT按钮是被禁用的,当选择一个项目时,它会启用NEXT按钮)
//全局 Button buttonPositiveInvolvement;
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) {                                            
                        //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;

0

您需要像这样在onClick中启用按钮:

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

这样就可以在选择任何选项时启用按钮。
希望这有所帮助。

我也尝试过了。由于我在“show()”方法之后初始化了buttonPositiveInvolvement,因此它显示“无法解析”。 - BRDroid
你是否已经在全局声明了 buttonPositiveInvolvement 变量? - Piyush
我也试过了。当我在对话框的开始处初始化它时。 最后一个按钮buttonPositiveInvolvement; 和 buttonPositiveInvolvement = alertInvolvement.getButton(AlertDialog.BUTTON_POSITIVE);就在show()之前。 并尝试在singleChoice的onClick中将其设置为TRUE, 它会说“remove final”,当我删除final时,它又会说“change modefier to final”。 - BRDroid
正如Piyush所说,将其设置为全局变量。因为你只需要在onClick中启用按钮。@user3046669 - MysticMagicϡ
AlertDialog alertInvolvement = builder.create(); buttonPositiveInvolvement = alertInvolvement.getButton(AlertDialog.BUTTON_POSITIVE); alertInvolvement.show();我按照你的建议做了,但是当我点击打开此对话框的按钮时出现了空指针异常。 - BRDroid
显示剩余3条评论

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
        }
    }
}

我已经搜索了很多......然后根据您的要求给出一些逻辑。我认为这段代码完全符合您的要求。


你好,我的需求是我有一个包含3个选项的singleChoiceItem。一开始NEXT按钮是禁用的,在我的程序中这个功能正常工作,但当选择一个选项后,我想启用NEXT按钮。我在这部分遇到了困难。 - BRDroid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接