【问题标题】:Disable positive button in alert dialog builder在警报对话框生成器中禁用肯定按钮
【发布时间】:2017-07-11 00:56:28
【问题描述】:

我创建了一个警报对话框构建器,在其中显示一个对话框我的肯定按钮名称是提交我希望禁用该按钮,除非表单中的所有字段都已填写。以下是我的代码,任何人都可以帮助我解决这。 谢谢

        alertDialogBuilder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    nameInput1 = data_txt1.getText().toString();
                    nameInput2 = data_txt2.getText().toString();
                    nameInput3 = data_txt3.getText().toString();
                    nameInput4 = data_txt4.getText().toString();
                    nameInput5 = data_txt5.getText().toString();

                    nameInput6 = auto_txt1.getText().toString();
                    nameInput7 = auto_txt2.getText().toString();
                    nameInput8 = auto_txt3.getText().toString();
                    nameInput9 = auto_txt4.getText().toString();
                    nameInput10 = auto_txt5.getText().toString();
                    nameInput11 = auto_txt6.getText().toString();
                    nameInput12 = auto_txt7.getText().toString();
                    nameInput13 = auto_txt8.getText().toString();
                    nameInput14 = auto_txt9.getText().toString();

                    nameInput15 = data_txt6.getText().toString();
                    nameInput16 = data_txt7.getText().toString();



                        Call<Void> completeQuestionnaireCall = spreadsheetWebService.completeQuestionnaire(nameInput1, nameInput2, nameInput3, nameInput4, nameInput5, nameInput6, nameInput7, nameInput8, nameInput9, nameInput10, nameInput11, nameInput12, nameInput13, nameInput14, nameInput15, nameInput16);
                        completeQuestionnaireCall.enqueue(callCallback);
                        dialog.dismiss();

                    }



                }
            });

    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();

        }
    });

    alertDialogBuilder.show();
}

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    创建一个方法来检查 Utils 类中的所有输入,或者可以在当前类中使用它

    public static boolean checkifEmptyText(String[] fields,Context context) {
        for (String currentField : fields) {
            if (currentField.getText().toString().trim().length() <= 0) {
                return false;
            }
        }
        return true;
    }
    

    现在像这样检查它

    if(checkifEmptyText(new String[]{all your strings}),context)
    {
     Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    button.setEnabled(true);
    }
    else
    {
    Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    button.setEnabled(false);
    }
    

    【讨论】:

      【解决方案2】:

      你也可以这样做:

      new AlertDialog.Builder(this)
          .setMessage("This may take a while")
          .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
               ((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);
               // the rest of your stuff
          }
      })
          .show();
      

      【讨论】:

        【解决方案3】:

        试试这个

        alertDialogBu​​ilder.create().getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

        【讨论】:

          【解决方案4】:

          你可以这样做:

              ....your code for alert dialog
              AlertDialog dialog = b.create(); // here b is reference of AlertDialog.Builder and in your case replace b with alertDialogBuilder
              dialog.show();
              Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
              button.setEnabled(false);
          

          希望这会有所帮助。

          【讨论】:

            【解决方案5】:

            正面按钮可以这样使用:

            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
            

            并且,对于否定按钮:

            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
            

            【讨论】:

              【解决方案6】:

              如果我正确理解了您的要求,

              您可以使用TextWatcher..

              它是如何工作的:
              将 textwatcher 添加为输入字段中文本更改的侦听器,它将确保动态检查那些被“填充”的内容,并且您可以关联按钮的可见性或相应地启用/禁用它

              有帮助的问题:
              1. Disable button when edittext fields are empty
              2. Use on text changed listener

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2017-07-21
                • 2012-01-03
                • 1970-01-01
                • 1970-01-01
                • 2011-02-01
                • 1970-01-01
                • 2014-04-12
                • 2019-03-20
                相关资源
                最近更新 更多