我使用的文本视图触摸监听器。我可以通过移动事件的触摸坐标。
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 I clicked.
例如,你好安卓
这是我的文字。现在,我可以得到XY坐标,但我可以得到字符索引,比方说,当我抚摸它。
This is my text. Now I can get the x y coordinates but can I get the character index, say A, when I touch it.
感谢
您必须在此改变onTouch()
You have to overide onTouch()
尝试使用以下
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;
}