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

标签 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;

非常感谢您的帮助。 谢谢

最佳答案

终于搞定了。感谢 Piyush 和 Mystic Magic 的帮助。在这里发布代码,可能会对其他人有所帮助。 全局初始化的buttonPositiveInvolvement 按钮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;

关于android - 在单选警报对话框中启用/禁用肯定按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21043230/

相关文章:

android - 如何在 Android 对话框中设置 ImageView?

android - AlertDialog 上缺少按钮 |安卓 7.0 (Nexus 5x)

android - 如何强制子 PreferenceScreen 包含在 Fragment 布局中 (Android)

android - RecyclerView 中项目中的工具提示(重叠在项目上方)

android - 无法更改操作栏中的图标颜色

java - 通过改造发布到 Google Cloud Messaging

android - 在 Android 中使用 metaio 在 openCV 中渲染 3D 对象

android - 在 ListViewItem 的按钮单击中创建 AlertDialog 时,如何从 AlertDialog 的单击处理程序获取 ListViewItem 的 ViewHolder?

android - AlertDialog 在 Android 6.0+ 上不会关闭

android - 关于卸载 Android 应用程序的警报