我正在尝试调整 PasswordTransformationMethod (https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/method/PasswordTransformationMethod.java),以便它可以与包含的字符序列的可编辑实例一起使用跨越。我想要的是转换后的字符串还包含源字符串中存在的跨度,例如样式跨度。这样做的原因是我只需要将部分输入转换为密码。例如,输入 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)
任何提示/指针都值得赞赏,因为这是一个文档非常稀疏的领域。