onTextChanged 时对自定义 AlertDialog 按钮的引用 returns null

reference to custom AlertDialog button returns null when onTextChanged

我正在开发一个为 AlertDialog 使用自定义视图的应用程序。我想让它在编辑文本字段为空时禁用提交按钮。我在网上找到了一些示例,但我在我的实现中一直在对提交按钮的引用上返回 null。只要我在编辑文本中输入任何文本,应用程序就会崩溃。这是 alertdialog class,我试图抓住按钮的失败被注释掉了:

public class FileNameDialogFragment extends DialogFragment {
private static final String TAG = "dialogFragment";

private EditText namefield;
private Button submit;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){

    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View v = inflater.inflate(R.layout.file_prompt_dialog, null, false);
    namefield = (EditText) v.findViewById(R.id.nameField);

    AlertDialog dialog = new AlertDialog.Builder(getActivity())
            .setTitle(R.string.dialog_title)
            .setView(v)
            .setIcon(R.drawable.icon)
            .setPositiveButton(R.string.btnOK, new DialogInterface.OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MainActivity.setFileName(namefield.getText().toString());
                    String fileName = namefield.getText().toString();
                    Toast.makeText(getActivity(), MainActivity.getUrl().toString(), Toast.LENGTH_SHORT).show();
                    MainActivity.download(MainActivity.getUrl(), getActivity(), MainActivity.getPath(), fileName);
                }
            })
            .setNegativeButton(R.string.btnCancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dismiss();
                }
            })
            .create();

            //I have also tried to place the code that is below here. neither works

    dialog.show();
    /*
     *  this is the section of code that i am having trouble with
     *
    submit = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE);
    namefield.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //left intentionally blank
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (count == 0){
                submit.setEnabled(false);
            }else{
                submit.setEnabled(true);<--NullPointerException
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            //left intentionally blank
        }
    });*/
    return dialog;
}
}

我希望找出如何使这项工作成为验证输入的方法。我不希望用户能够将此字段留空,因为输入用于命名从 Internet 下载的文件。

使用 v.findViewByID() 找到您的按钮,然后使按钮在 textListener 中不可见或可见。希望对你有帮助。

submit.setVisibility(View.INVISIBLE);

您可以尝试下面的代码,我已经指定了如何根据您的 editText

禁用自定义按钮以及对话框的 positive/negative 按钮
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View v = inflater.inflate(R.layout.file_prompt_dialog, null, false);
    namefield = v.findViewById(R.id.nameField);

    //To get custom button of dialog
    submit = v.findViewById(R.id.submitBtn);

    final AlertDialog dialog = new AlertDialog.Builder(getActivity())
            .setTitle(R.string.dialog_title)
            .setView(v)
            .setIcon(R.mipmap.ic_launcher)
            .setPositiveButton(R.string.btnOK, new DialogInterface.OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MainActivity.setFileName(namefield.getText().toString());
                    String fileName = namefield.getText().toString();
                    Toast.makeText(getActivity(), "Positive Button", Toast.LENGTH_SHORT).show();
                    MainActivity.download(MainActivity.getUrl(), getActivity(), MainActivity.getPath(), fileName);
                }
            })
            .setNegativeButton(R.string.btnCancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dismiss();
                }
            })
            .create();

    dialog.show();

    //Dialog's positive button
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

    //Custom button
    submit.setEnabled(false);

    namefield.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //left intentionally blank
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (count == 0){

                //To disable dialog's positive button
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

                //To disable custom button
                submit.setEnabled(false);
            }else{

                //To enable dialog/s positive button
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);

                //To enable custom button
                submit.setEnabled(true);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            //left intentionally blank
        }
    });

    return dialog;
}

您需要先找到该按钮,然后才能使用 @Ojas 所说的 v.findViewById 进行分配。 而如果你想被禁用又不知道让它完全隐形有多好。用这段代码试试

    buttonRight.setEnabled(false);
    buttonRight.setAlpha(0.5f);

(这将禁用它并且 setAlpha 将使其半透明,这将使其看起来像一个禁用按钮)。

也不要使用 count == 0 试试 TextUtils.isEmpty(namefield.getText().toString()) 这是一个更干净的解决方案,是一种很好的使用习惯。