Android在addTextChangedListener(editext)中的某些条件上禁用了正按钮

前端之家收集整理的这篇文章主要介绍了Android在addTextChangedListener(editext)中的某些条件上禁用了正按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的对话框代码,如果编辑文本中的文本大小大于5,我想禁用正按钮,如果大小< = 5则启用它
private void myDialog(String title) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);


            // Get the layout inflater
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View dialogView = inflater.inflate(R.layout.new_dialog,null);

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(dialogView);

            final EditText dialogEdittext = (EditText) dialogView.findViewById(R.id.dialog_editText);
            final TextView dialogMessage = (TextView) dialogView.findViewById(R.id.dialog_limit);

            dialogEdittext.addTextChangedListener(new TextWatcher() {

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

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

                @Override
                public void afterTextChanged(Editable s) {

                    // if text length is greater than 5 disable positive button
                                // else enable 
                }
            });

            builder.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,int id) {

            });

            builder.setNegativeButton("Cancel",int id) {
                }
            });

            final Dialog dialog = builder.create();
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            dialog.show();

        }

解决方法

尝试这样的事情:
@Override
public void afterTextChanged(Editable s) {
     if (s.length() > 5) {
        dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
     } else {
        dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(true);
     }
}

对话框是:

final AlertDialog dialog = builder.create();

猜你在找的Android相关文章