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 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)
任何想法为什么会发生?以及如何解决这个问题?
解决方法 在此代码段中,您缩短EditText中显示的文本(以便最后一个有效位置为start-1),但仍尝试将选择位置设置为start.text = text.substring(0,start); mEditPass.setText(text); mEditPass.setSelection(start);
[EDITED]
假设您的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 after) { } @OverrIDe public voID onTextChanged(CharSequence s,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全部内容,希望文章能够帮你解决android – IndexOutOfBoundsException setSpan(0 … 1)结束超过0所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)