如何仅从 EditText 获取可见文本

编程入门 行业动态 更新时间:2024-10-28 01:13:01
本文介绍了如何仅从 EditText 获取可见文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个用于在 Android 上运行的代码编辑器的 EditText.我正在对内容应用语法突出显示,它运行良好,除了 EditText 在屏幕外滚动的大字符串 - 每次按键时语法突出显示都会应用于整个 Spannable.

I have an EditText for a code editor running on Android. I'm applying syntax highlighting to the content, it's working well except for large Strings where the EditText scrolls off screen - the syntax highlighting is being applied to the entire Spannable on every key press.

有谁知道我将如何只捕获用户可见的文本?

Does anyone know how I'd go about capturing only the text that is visible to the user?

我只能想到基于光标位置的讨厌的黑客,但当用户滚动时会中断.

I can only think of nasty hack based on cursor position but that would break when the user scrolls.

推荐答案

可以使用下面的函数来获取可见的文本偏移量.您将输入编辑文本的原始 x,y 角,它将返回最近的文本偏移量(即字符位置).

Can use the function below to get the visible text offsets. You'd input the raw x,y corners of the edit text, and it will return the nearest text offset (i.e. character position).

    EditText et = ;
    et.getOffsetForPosition(x, y);

一旦您知道可见文本的开始/结束,您就可以相应地设置跨度.如果你只是改变颜色(即不是高度/宽度),那么不应该有任何循环依赖.

Once you know start / end of the visible text, you set your span accordingly. If you are just changing coloring (i.e. not height / width) then there shouldn't be any circular dependency.

这篇关于如何仅从 EditText 获取可见文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 03:25:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034323.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   EditText

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!