9

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

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

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

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

4

3 回答 3

8

我将 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) {

    }
});
于 2013-09-06T10:08:33.670 回答
4

我认为最简单的方法是在 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
   }

});
于 2010-05-09T21:30:22.157 回答
1

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

在您的 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
    }
    }
});
于 2011-09-01T10:16:06.637 回答