如何仅从 EditText 获取可见文本 [英] How to get only visible text from a EditText

查看:29
本文介绍了如何仅从 EditText 获取可见文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆