EditText PasswordtransformationMethod和span

时间:2013-08-25 19:23:09

标签: android android-layout textview android-edittext spannablestring

我正在尝试调整PasswordTransformationMethod(https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/method/PasswordTransformationMethod.java),以便它可以使用包含跨度的可编辑的charsequence实例。我想要的是变换后的字符串还包含源字符串中存在的跨度,例如造型跨度。这样做的原因是我只需要将部分输入转换为密码。例如,输入ABCDEF将在文本输入期间转换为AB ... F并保留任何样式范围。 看看上面的PasswordTransformationMethod,看起来PasswordCharSequence只实现了Charsequence和GetChars,从而丢失了源字符串上的任何跨度。

从上面的android源代码链接,我试图更改PasswordCharSequence,以便它扩展SpannableStringBuilder,但是这给出了以下异常:

08-25 19:41:29.162: E/AndroidRuntime(998): java.lang.IndexOutOfBoundsException
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.graphics.Paint.getTextRunAdvances(Paint.java:1734)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.graphics.Paint.getTextRunAdvances(Paint.java:1711)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.text.TextLine.handleText(TextLine.java:749)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.text.TextLine.handleRun(TextLine.java:1017)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.text.TextLine.measureRun(TextLine.java:416)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.text.TextLine.measure(TextLine.java:295)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.text.Layout.getHorizontal(Layout.java:1145)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.text.Layout.getHorizontal(Layout.java:1123)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.text.Layout.getPrimaryHorizontal(Layout.java:1107)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.widget.TextView.bringPointIntoView(TextView.java:7224)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.widget.TextView.updateAfterEdit(TextView.java:6343)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.widget.TextView.finishBatchEdit(TextView.java:6324)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.widget.TextView.endBatchEdit(TextView.java:6307)
08-25 19:41:29.162: E/AndroidRuntime(998):  at com.android.internal.widget.EditableInputConnection.endBatchEdit(EditableInputConnection.java:60)
08-25 19:41:29.162: E/AndroidRuntime(998):  at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:436)
08-25 19:41:29.162: E/AndroidRuntime(998):  at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.os.Looper.loop(Looper.java:154)
08-25 19:41:29.162: E/AndroidRuntime(998):  at android.app.ActivityThread.main(ActivityThread.java:4945)
08-25 19:41:29.162: E/AndroidRuntime(998):  at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:41:29.162: E/AndroidRuntime(998):  at java.lang.reflect.Method.invoke(Method.java:511)
08-25 19:41:29.162: E/AndroidRuntime(998):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-25 19:41:29.162: E/AndroidRuntime(998):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-25 19:41:29.162: E/AndroidRuntime(998):  at dalvik.system.NativeStart.main(Native Method)

任何提示/指针都很受欢迎,因为这是一个文档非常稀疏的区域。

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题,除了Charsequnce和GetChars之外,还让PasswordCharSequence实现了Spannable,并通过将这些方法委托给mSource实现了所有的spannable方法。 mSource.getSpans(...)。现在很好地将跨度应用到转换后的文本上,从而很好地满足了我的目的!