在TextView中触摸坐标 [英] Touch coordinates in textview
本文介绍了在TextView中触摸坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的文本视图触摸监听器。我可以通过移动事件的触摸坐标。
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;
}
这篇关于在TextView中触摸坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文