【问题标题】:Validating Dialog Input验证对话框输入
【发布时间】:2010-05-09 21:20:29
【问题描述】:

我正在使用一个非常简单的 AlertDialog,只是一个带有文本框的自定义视图,以及 alertdialog 肯定提交按钮。我想验证文本框是否在用户关闭对话框之前输入了文本。我看到了两种可能性,每个都有问题:

  • 禁用提交按钮,直到 文本框不为空(在某些 文本的 onChange() 类型处理程序 盒子)
    • 如何知道文本框的内容何时发生变化?
    • 如何获得对 AlertDialog 按钮对象的引用?
  • 签入提交按钮的 onClick() 并取消解雇 如果对话框为空,则显示对话框。
    • 是否可以使用 AlertDialog 的按钮来执行此操作?对话框在没有手动调用dismiss() 或cancel() 的情况下关闭,所以我不确定...

AlertDialog(与自定义对话框相比)是否可以使用这些选项中的任何一个?

我认为第二个选项是最简单的,但如果可能的话,我愿意。

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    我将 TextWatcher 设置为输入字段,然后在满足验证条件时启用肯定按钮。我默认禁用正面按钮。

    AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
    final View dialogView = LayoutInflater.from(getSherlockActivity()).inflate(R.layout.create_playlist, null);
    final EditText inputField = (EditText) dialogView.findViewById(R.id.edit_newPlaylistName);
    ... //proceed to setup dialog using builder
    final AlertDialog alertDialog = builder.show();
    alertDialog.setCanceledOnTouchOutside(true);
    final Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    button.setEnabled(false);
    inputField.addTextChangedListener(new TextWatcher() {
        @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // my validation condition
            if (inputField.getText().length() > 0) {
               button.setEnabled(true);
            } else {
               button.setEnabled(false);
            }
        }
    
        @Override
        public void afterTextChanged(Editable s) {
    
        }
    });
    

    【讨论】:

      【解决方案2】:

      我认为最简单的方法是在 xml 文件中定义自己的对话框。然后你可以很简单地显示它(在这个例子中,在活动类的某个地方):

      Dialog dialog = new Dialog(this);
      dialog.setContentView(R.layout.your_dialog_file);
      
      Button yourButton = dialog.findViewById(R.id.yourButton);
      final EditText text = dialog.findViewById(R.id.yourTextEdit);
      yourButton.setOnClickListener( {
      
      public void onClick(View view) {
         if ( ! (text.getText().toString.equals(""))) {
              //go on here and dismiss dialog
         }
      
      });
      

      【讨论】:

        【解决方案3】:

        这是一种验证输入并在输入无效时在屏幕上保留警告对话框的方法。实际上,它会删除对话框并为其创建一个新副本。

        在您的 setPostiveButton 的 onClick 函数中,进行验证。 如果事情不是他们应该的样子,向用户展示一个 Toast。 然后在您的对话框上调用 removeDialog 。 然后 - 这是棘手的部分,在您的对话框上异步调用 showDialog(如果适用,使用 args)。 另外:为了不丢失用户的输入,您应该将他们输入的值放入您调用对话框的包中。当然,您的对话框设置代码需要在包中查找这些值并适当地预填充对话框字段。

        所以你的代码看起来像这样:

        alert.setPositiveButton(id,
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) 
            {
            if ((your validation conditions)) {
                // validation conditions not satisfied
                removeDialog(myDialogId);
                Toast.makeText(blah blah).show();
                // now create a new args bundle for the dialog
                Bundle newArgs = new Bundle();
                // now copy whatever you need from the args used to invoke to dialog
                newArgs.putIntegerArrayList("items", myList);
                // now save the user's input in a bundle
                newArgs.putString("dialogToFieldContent", toString);
                final Bundle finalArgs = newArgs;
                Handler handler = new Handler();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                    showDialog(myDialogId, finalArgs);
                    }
                });
            }
            else {
                // if everything is ok
            }
            }
        });
        

        【讨论】:

        • 我从哪里获得对话ID?您能否提供一个最小的功能示例!?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多