android - 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("");
}
}
}
}
});
}
它仅在某些手机上不起作用
解决方案
删除 XML 的输入类型和数字属性
推荐阅读
- css-filters - 更改页面上另一个元素的混合混合模式时,背景过滤器不起作用
- python - GofChisquarePower solve_power
- angular - Angular 材质 matNoDataRow 在 Angular 11 中不起作用
- c++ - С++ 抽象返回类型
- mongodb - 使用 UUID v4 的键集分页
- asp.net - ASP.NET 中的更新导致异常:“必须声明标量变量”
- python-3.x - 使用 n 个唯一整数生成不带替换的随机整数以获得固定总和(Python)
- arrays - 将数组添加到 Angular 中的数组
- python - 如何从 3D 制作 2D ndarray 以使 (100, 50, 20) 为 (100, 100)
- html - CSS图标故障?