我有一个包含可编辑文本的 TextView。这段文字中有图片(drawables,我用的是Html.ImageGetter),我想注册onTouch事件。因此,当用户触摸作为可跨文本一部分的图像时,我想知道这一点。(而且,他触摸了哪个图像)。
那可能吗?如果是这样,也许没有肮脏的解决方法?
最好的问候,扬·奥利弗·奥勒里奇
我有一个包含可编辑文本的 TextView。这段文字中有图片(drawables,我用的是Html.ImageGetter),我想注册onTouch事件。因此,当用户触摸作为可跨文本一部分的图像时,我想知道这一点。(而且,他触摸了哪个图像)。
那可能吗?如果是这样,也许没有肮脏的解决方法?
最好的问候,扬·奥利弗·奥勒里奇
从 ArrowKeyMovementMethod 源代码复制,您可以尝试使用以下代码片段:
private int getOffset(int x, int y, TextView widget){
// Converts the absolute X,Y coordinates to the character offset for the
// character whose position is closest to the specified
// horizontal position.
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
// Clamp the position to inside of the view.
if (x < 0) {
x = 0;
} else if (x >= (widget.getWidth()-widget.getTotalPaddingRight())) {
x = widget.getWidth()-widget.getTotalPaddingRight() - 1;
}
if (y < 0) {
y = 0;
} else if (y >= (widget.getHeight()-widget.getTotalPaddingBottom())) {
y = widget.getHeight()-widget.getTotalPaddingBottom() - 1;
}
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int offset = layout.getOffsetForHorizontal(line, x);
return offset;
}
希望对您有所帮助,EdenSphere。