首页 > 解决方案 > EditText 失去对输入特殊字符的关注

问题描述

我收到奇怪的错误

我有一个密码字段的 Edittext

<EditText
                    android:id="@+id/etNewPassword"
                    style="@style/EditTextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/hint_new_password"
                    android:inputType="textPassword|textNoSuggestions"
                    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^*"
                    android:maxLength="16"
                    android:textSize="14dp" />

假设我想在该 editText 字段中输入Password@123 。它不会在@ 之后输入我的值。它在输入@ 后失去焦点然后我需要再次单击相同的 EditText 并输入值 123。

但如果我输入@123,它的工作。即使我输入 @Pass 它的工作。只有当我尝试组合 Letters-SpecialCharacter-numbers 时,它才不起作用。它仅在某些设备上发生。

这是我的 Java 代码。

etPassword.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) {

            }

            String str = "";

            @Override
            public void afterTextChanged(Editable s) {

                String text = s.toString();
                str = s.toString();
                if (etPassword.getText().toString().length() > 0 && etPassword.getText().toString().equals(" ") && etPassword.getText().toString().equals("^[@.#$%^&*_&\\\\\\\\]+$")) {
                    etPassword.setText("");
                } else {
                    if (Utils.validateNotBlank(etPassword.getText().toString())) {
                        Pattern p = Pattern.compile("[A-Za-z0-9]+");
                        Matcher m = p.matcher(text);
                       //boolean b = m.matches();


                        if (some_ID_validation) {

                            String editTextString = etPassword.getText().toString();
                            boolean spaceExist = false, specialExist = false;

                            if (editTextString.contains(" ")) {

                                text = etPassword.getText().toString().replaceAll(" ", "");
                                if (!str.equals(text)) {
                                    str = text;

                                    spaceExist = true;
                                    etPassword.setText(text);
                                    etPassword.setSelection(text.length());

                                }

                            }
                            if (!m.matches()) {

                                text = etPassword.getText().toString().replaceAll("[^a-zA-Z0-9]+", "");


                                if (!str.equals(text)) {
                                    specialExist = true;
                                    str = text;
                                    etPassword.setText(text);
                                    etPassword.setSelection(text.length());

                                }
                            }



                    Pattern p = Pattern.compile("[^-\\s]");
                    Matcher m = p.matcher(text);
                    boolean b = m.matches();
                    if (etPassword.getText().toString().contains(" ") && !b) {

                        text = text.replaceAll(" ", "");
                        Util.showToast("Spaces not allowed in password field", activity);
                        if (!str.equals(text)) {
                            str = text;

                            etPassword.setText(text);
                            etPassword.setSelection(text.length());
                        }
                    }
                    if (text.length() > 0) {
                        //setPasswordStrengthMsg(Util.getPasswordStrength(text));

                    } else {
                        if (!str.equals(text)) {
                            str = text;
                            etPasswordStrength.setText("");
                        }
                    }

                }
            }


        });

    }

它仅在某些手机上不起作用

标签: androidandroid-edittextpasswords

解决方案


删除 XML 的输入类型和数字属性


推荐阅读