在onTextChanged时,对自定义AlertDialog按钮的引用返回null

时间:2017-07-20 06:14:57

标签: android android-alertdialog addtextchangedlistener

我正在开发一个使用AlertDialog的自定义视图的应用程序。我想这样做,以便在编辑文本字段为空时禁用提交按钮。我在网上找到了一些例子但是我的实现在对提交按钮的引用上保持返回null。只要我在edittext中输入任何文本,应用程序就会崩溃。这是alertdialog类,我没有尝试抓住按钮注释掉:

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下载的文件。

3 个答案:

答案 0 :(得分:1)

您可以尝试下面的代码,我已经指定了如何禁用自定义按钮以及对话框的正/负按钮,具体取决于您的editText

@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;
}

答案 1 :(得分:1)

你需要找到那个按钮才能分配使用@Ojas v.findViewById。 如果你想要被禁用,并且不知道它有多么好,使它完全不可见。尝试使用此代码

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

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

并且也不要使用count == 0尝试TextUtils.isEmpty(namefield.getText().toString())这是一个更清洁的解决方案,这是一个很好的做法。

答案 2 :(得分:0)

使用v.findViewByID()查找按钮,然后在textListener中使按钮不可见或可见。希望它可以帮到你。

submit.setVisibility(View.INVISIBLE);