16

我有TextView很多ClickableSpan.

单击 aClickableSpan时,我必须在其屏幕上获取坐标(以View在他的位置显示自定义)。

问题是我不知道如何做到这一点。的onClick()方法ClickableSpan在参数 a 中给了我ViewTextView其中包含ClickableSpan.

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

@Override
public void onClick(View v){

    SpannableString completeText = (SpannableString)((TextView) v).getText();
    Log.v("characters position", completeText.getSpanStart(this) + " / " + completeText.getSpanEnd(this));

}

在此先感谢您的帮助!

编辑:我想获取 ClickableSpan 的整个坐标及其大小,目的是在文本的底部中心显示我的自定义视图。onTouch 方法会给我手指位置,而不是整个文本坐标。所以,用这个方法,我就不会有中间的文字了。

4

2 回答 2

34

我找到了解决方案 :-) 就是这样,这可能会帮助像我一样遇到同样问题的其他人!

// 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
);
于 2012-08-13T12:33:59.073 回答
0

试试这个:

在 CustomSpannableString 类 onClick 函数内部

@Override
public void onClick(View v){
    val textView = v as TextView
    val s = textView.text as Spanned
    val startCoordinates = s.getSpanStart(this)
    val endCoordinates = s.getSpanEnd(this)
    return  arrayOf(startCoordinates, endCoordinates)
}

不在 spannableString 类中

val textView = findView...   // txtView which text is set to SpannableString
val s = textView.text as Spanned 
// CustomSpannableStringObj of type CustomSpannableString 
val startCoordinates = s.getSpanStart(CustomSpannableStringObj)
val endCoordinates = s.getSpanEnd(CustomSpannableStringObj)
于 2021-09-17T14:37:34.770 回答