Android: How to determine character index of a touch event's position in TextView?

后端 未结 3 1739
陌清茗
陌清茗 2020-12-06 03:04

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 t

3条回答
  •  清歌不尽
    2020-12-06 03:08

    Have you tried something like this:

    Layout layout = this.getLayout();
    if (layout != null)
    {
        int line = layout.getLineForVertical(y);
        int offset = layout.getOffsetForHorizontal(line, x);
    
        // At this point, "offset" should be what you want - the character index
    }
    

    Hope this helps...

提交回复
热议问题