Touch coordinates in textview

后端 未结 1 1962
你的背包
你的背包 2021-01-03 04:05

I am using touch listener on text view. I can get the touch coordinates through motion event.

Can I get the character index or near by character coordinates on which

1条回答
  •  北海茫月
    2021-01-03 04:42

    You have to overide onTouch()

    Try with the following

    public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
             Layout layout = ((TextView) v).getLayout();
                int x = (int)event.getX();
                int y = (int)event.getY();
                if (layout!=null){
                    int line = layout.getLineForVertical(y);
                    int characterOffset = layout.getOffsetForHorizontal(line, x);
                    Log.i("index", ""+characterOffset);
                    }
                return true;
    
    
        }
    

    0 讨论(0)
提交回复
热议问题