Android 开发:将 setText 与 SpannableString 一起使用,由于 Spans 较多,速度很慢,有解决方法吗?

发布于 2024-12-02 20:46:52 字数 325 浏览 9 评论 0原文

我为我的应用程序制作了一个小语法荧光笔。

它执行正则表达式并将 ForegroundColorSpans 应用于 EditText 内容的副本 - 所有这些都在一个线程中。完成所有这些后,将调用 setText() 方法,并将 EditText 的上下文替换为现在样式的副本。

问题是,setText() 方法会导致 UI 冻结。在我对 2,000 行文件进行的测试中,应用样式文本需要 30 秒!

除了建议用户不要对大文件使用语法突出显示并在打开大文件时自动将其关闭之外,我还能做些什么来加快语法突出显示的速度?除了减少正则表达式并使之更加高效之外。

谢谢, 亚历克斯.

I've made a little syntax highlighter for my app.

It performs the regular expressions and applies the ForegroundColorSpans to a copy of the EditText's contect - all in a thread. Once all this is done, the setText() method gets called and replaces the EditText's context with the now-styled copy.

Problem is, the setText() method causes the UI to freeze. In a test I did with a 2,000 line file it took 30 seconds to apply the styled text!

Aside from recommending users that the syntax highlighting shouldn't be used with big files and automatically turning it off if they are opening a big file, what can I do to speed the syntax highlighting up? Apart from reducing and making the regular expressions more efficient.

Thanks,
Alex.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔芯 2024-12-09 20:46:53

也许您可以使用 WebView 或者尝试开发一些功能,以便仅对用户可见的文本进行样式设置。

Maybe you can use a WebView or alternatively try to develop some functionality so that only the text visible to the user is styled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文