Android的:如何确定TextView的触摸事件的位置的字符索引? [英] Android: How to determine character index of a touch event's position in TextView?
问题描述
我有一个的TextView
与 OnTouchListener
。我要的是人物索引的用户指向时,我得到了 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
?
推荐答案
我不知道一个简单直接的方式来做到这一点,但你应该能够使用的通过调用的对象 /android/widget/TextView.html#getPaint%28%29相对=nofollow> 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屋!