gpt4 book ai didi

android - IndexOutOfBoundsException setSpan (0 ... 1) 超出长度 0 结束

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:36:30 24 4
gpt4 key购买 nike

我将这个库用于带有标签的 Material editText: https://github.com/rey5137/Material/wiki/Text-Field不错的图书馆:)

但是……

我正在使用下一个代码来检查输入的符号是否正确:

private boolean hasCorrectSymbols(String input){
String tre = "[A-Za-z0-9\\@\\#\\$\\%\\&\\*\\(\\)\\-\\+\\_\\;\\:\\?\\.\\,\\!]+$";
if (input.matches(tre)){
return true;
}
return false;
}

我使用 textWatcher 检查正确的符号:

mEditPass.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) {
if (s.length() == 1 && !loginPassHasCorrectSymbols(s.toString())){
mEditPass.getText().clear();
String mess = getString(R.string.toast_login_useLatin);
showToastMessage(mess);
} else if (s.length() >1 && !loginPassHasCorrectSymbols(s.toString())) {
String mess = getString(R.string.toast_login_useLatin);
showToastMessage(mess);
String text = s.toString();
text = text.substring(0, start);
mEditPass.setText(text);
mEditPass.setSelection(start);
}
}

@Override
public void afterTextChanged(Editable s) {}
});

如果第一个符号是正确的而第二个是错误的 - 系统将剪切最后输入的(错误的)符号并将光标设置到最后一个文本位置,但如果第一个符号是禁止符号 - 它会因此错误而崩溃:

java.lang.IndexOutOfBoundsException: setSpan (0 ... 1) ends beyond length 0
at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
at android.text.method.PasswordTransformationMethod.onTextChanged(PasswordTransformationMethod.java:108)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:683)
at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:198)
at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5097)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)

知道为什么会这样吗?以及如何解决这个问题??

最佳答案

在此代码 fragment 中,您缩短了 EditText 中显示的文本(以便最后一个有效位置为 start-1),但仍尝试设置选择开始的位置。

text = text.substring(0, start);
mEditPass.setText(text);
mEditPass.setSelection(start);

[编辑]

假设您的 Toast 消息提示用户修复错误,我认为您真正想要做的是将选择设置为 EditText 中带有错误符号的第一个字符。这是执行此操作的示例代码:

// A reusable Pattern (at the class level) that defines the regex for bad characters.
private static final Pattern ILLEGAL_CHAR_PATTERN = Pattern.compile(
"[^A-Za-z0-9\\@\\#\\$\\%\\&\\*\\(\\)\\-\\+\\_\\;\\:\\?\\.\\,\\!]"
);

// Replacement for your listener code.
mEditPass.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) {
if (s.length() == 0 || count == 0) {
return;
}
Matcher matcher = ILLEGAL_CHAR_PATTERN.matcher(s);
if (matcher.find()) {
int firstIllegalPos = matcher.start();
mEditPass.setSelection(firstIllegalPos);
String mess = getString(R.string.toast_login_useLatin);
showToastMessage(mess);
}
}

@Override
public void afterTextChanged(Editable s) {
}
});

关于android - IndexOutOfBoundsException setSpan (0 ... 1) 超出长度 0 结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32876124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com