如何在TextView中获取ClickableSpan的坐标?

时间:2012-08-10 16:12:36

标签: android textview position clickable

我有TextViewClickableSpan

点击ClickableSpan,我必须在屏幕上显示坐标(在他的位置显示自定义View)。

问题在于我不知道如何做到这一点。 onClick()的{​​{1}}方法为我提供参数a ClickableSpanView包含TextView

我使用以下内容在ClickableSpan中获取字符位置,但我不知道如何将其转换为在文本屏幕上获取x / y位置。

TextView

提前感谢您的帮助!

编辑:我想获得ClickableSpan的整个坐标及其大小,目的是在文本的底部中心显示我的自定义视图。 onTouch方法将为我提供手指位置,而不是整个文本坐标。所以,使用这种方法,我不会有文本的中间部分。

1 个答案:

答案 0 :(得分:28)

我找到了一个解决方案:-) 在这里,这可能会帮助其他人遇到与我相同的问题!

// Initialize global value
this.parentTextViewRect = new Rect();


// Initialize values for the computing of clickedText position
SpannableString completeText = (SpannableString)(parentTextView).getText();
Layout textViewLayout = parentTextView.getLayout();

double startOffsetOfClickedText = completeText.getSpanStart(clickedText);
double endOffsetOfClickedText = completeText.getSpanEnd(clickedText);
double startXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal((int)startOffsetOfClickedText);
double endXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal((int)endOffsetOfClickedText);


// Get the rectangle of the clicked text
int currentLineStartOffset = textViewLayout.getLineForOffset((int)startOffsetOfClickedText);
int currentLineEndOffset = textViewLayout.getLineForOffset((int)endOffsetOfClickedText);
boolean keywordIsInMultiLine = currentLineStartOffset != currentLineEndOffset;
textViewLayout.getLineBounds(currentLineStartOffset, this.parentTextViewRect);


// Update the rectangle position to his real position on screen
int[] parentTextViewLocation = {0,0};
parentTextView.getLocationOnScreen(parentTextViewLocation);

double parentTextViewTopAndBottomOffset = (
    parentTextViewLocation[1] - 
    parentTextView.getScrollY() + 
    this.parentTextView.getCompoundPaddingTop()
);
this.parentTextViewRect.top += parentTextViewTopAndBottomOffset;
this.parentTextViewRect.bottom += parentTextViewTopAndBottomOffset;

// In the case of multi line text, we have to choose what rectangle take
if (keywordIsInMultiLine){

    int screenHeight = this.mWindowManager.getDefaultDisplay().getHeight();
    int dyTop = this.parentTextViewRect.top;
    int dyBottom = screenHeight - this.parentTextViewRect.bottom;
    boolean onTop = dyTop > dyBottom;

    if (onTop){
        endXCoordinatesOfClickedText = textViewLayout.getLineRight(currentLineStartOffset);
    }
    else{
        this.parentTextViewRect = new Rect();
        textViewLayout.getLineBounds(currentLineEndOffset, this.parentTextViewRect);
        this.parentTextViewRect.top += parentTextViewTopAndBottomOffset;
        this.parentTextViewRect.bottom += parentTextViewTopAndBottomOffset;
        startXCoordinatesOfClickedText = textViewLayout.getLineLeft(currentLineEndOffset);
    }

}

this.parentTextViewRect.left += (
    parentTextViewLocation[0] +
    startXCoordinatesOfClickedText + 
    this.parentTextView.getCompoundPaddingLeft() - 
    parentTextView.getScrollX()
);
this.parentTextViewRect.right = (int) (
    this.parentTextViewRect.left + 
    endXCoordinatesOfClickedText - 
    startXCoordinatesOfClickedText
);