Android:如何确定TextView中触摸事件位置的字符索引? [英] Android: How to determine character index of a touch event's position in TextView?
问题描述
我有一个带有 OnTouchListener
的 TextView
.我想要的是当我得到 MotionEvent
时用户指向的字符索引.有没有办法获得 TextView
的底层字体指标?
I have a TextView
with an OnTouchListener
. What I want is the character index the user is pointing to when I get the MotionEvent
. Is there any way to get to the underlying font metrics of the TextView
?
推荐答案
我不知道有一种简单的直接方法可以做到这一点,但您应该能够使用 Paint 对象通过调用 TextView.getPaint()
I am not aware of a simple direct way to do this but you should be able to put something together using the Paint object of the TextView via a call to TextView.getPaint()
一旦你有了画图对象,你就可以通过调用 Paint.getFontMetrics() 来访问底层的 FontMetrices,并可以访问其他函数,比如 Paint.measureText() Paint.getTextBounds() 和 Paint.getTextWidths()用于访问显示文本的实际大小.
Once you have the paint object you will have access to the underlying FontMetrices via a call to Paint.getFontMetrics() and have access to other functions like Paint.measureText() Paint.getTextBounds(), and Paint.getTextWidths() for accessing the actual size of the displayed text.
这篇关于Android:如何确定TextView中触摸事件位置的字符索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!