'Android/TextView'에 해당되는 글 1건

  1. 2013.08.19 TextView text 터치한 부분 index 가져오기
2013. 8. 19. 15:30

So I ended up solving this via the link in James Moore's comment here. I ended up using the Tony Blues's solution in that link. That solution is this:

1.) Set an onTouchListener to the TextView.

2.) Get the layout of the TextView.

3.) Get the x,y coordinates of the touch via the event.

4.) Find the line and offset (index) based on that event.

The following code example is how one would Log (in verbose) the index of a touch given a TextView named manip:

manip.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
    Layout layout = ((TextView) v).getLayout();
    int x = (int)event.getX();
    int y = (int)event.getY();
    if (layout!=null){
        int line = layout.getLineForVertical(y);
        int offset = layout.getOffsetForHorizontal(line, x);
        Log.v("index", ""+offset);
        }
    return true;
}
});


출저 : http://stackoverflow.com/questions/6285682/onclick-position-in-string-in-textview

Posted by hoonihoon