android - 如何获取 TextView 中 ClickableSpan 的坐标?

标签 android textview position clickable

我有一个包含许多 ClickableSpanTextView

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

问题是我不知道如何做到这一点。 ClickableSpanonClick() 方法在参数中为我提供了一个 View,即包含 TextView >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 的整个坐标及其大小,目的是在文本的底部中心显示我的自定义 View 。 onTouch 方法会给我手指位置,而不是整个文本坐标。所以,用这个方法,我不会有文本的中间部分。

最佳答案

我找到了解决办法:-) 在这里,这可能会帮助像我一样遇到同样问题的其他人!

// 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
);

关于android - 如何获取 TextView 中 ClickableSpan 的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11905486/

相关文章:

html - 相对于表格单元格的定位

html - 在较短的页面上,网页页脚未到达页面底部

android - 使用命令提示符构建 Android 应用程序

android - 扩展 Android 蓝牙可发现性

android - 向自定义 TextView 添加文本是不可见的

java - 使用反射方法设置EditText光标颜色

python - 使用 Python 中的 re 模块查找模式的位置

android - Android Studio 中的“恢复文件”菜单

android - 尝试将 google pay 集成到我的 Android 应用程序中

android - TextView 内文本的偏心对齐