how to handle empty text from the input alert dialog and control the alert button

问题: I have an alert dialog and I have an input from the alert dialog. The current issue is I cant handle the input from the alert dialog if the user didnt input any text and cl...

问题:

I have an alert dialog and I have an input from the alert dialog. The current issue is I cant handle the input from the alert dialog if the user didnt input any text and click ok button. How to set the textfield showing error if user didnt input any text and click OK button and without closing the alert dialog. Below is my code.

 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setTitle("Save QR Code");
    builder.setMessage("Please enter the name for the QR Code.");
    builder.setCancelable(true);

    // Set up the input
    final EditText input = new EditText(getContext());
    input.setInputType(InputType.TYPE_CLASS_TEXT );
    builder.setView(input);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Here is the part
            m_Text = input.getText().toString().trim();
            if(TextUtils.isEmpty(m_Text)){
                input.setError("Please Enter Name of the Image QR Code");
            }else {
                //do smtg
            }
        }
    });
    builder.show();

回答1:

You should set the listener later:

final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setTitle("Save QR Code");
    builder.setMessage("Please enter the name for the QR Code.");
    builder.setCancelable(true);
// Set up the input
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT );
builder.setView(input);

builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(view1 -> { 
        m_Text = input.getText().toString().trim();
        if(TextUtils.isEmpty(m_Text)){
            input.setError("Please Enter Name of the Image QR Code");
        } else {
            dialog.dismiss();
        }
});

The problem with your approach is that AlertDialog will still use its listener as a kind of decorator of yours. So it will call your listener but then it will still close the dialog. If you want to prevent it, you need to overwrite the internal listener with yours after the dialog is shown.


回答2:

Add a TextWatcher to the EditText. When the text is empty, show the error message. You can even hide the PositiveButton from within the TextWatcher to prevent the user from hitting it when the input is invalid.

        myEditText.addTextChangedListener(new TextWatcher() {
        boolean bIgnore = false; // indicates if the change was made by the TextWatcher itself.

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (bIgnore)
                return;

            bIgnore = true; // prevent infinite loop

            if (inputValidated(myEditText.getText().toString()) {
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
                errorTextView.setVisibility(View.INVISIBLE);

            } else {
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
                errorTextView.setVisibility(View.VISIBLE);

            }

            bIgnore = false; // release, so the TextWatcher start to listen again.
        }
    });
  • 发表于 2019-03-18 23:22
  • 阅读 ( 244 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除