如何在 TextView 中获取 ClickableSpan 的坐标? [英] How get coordinate of a ClickableSpan inside a TextView?

查看:14
本文介绍了如何在 TextView 中获取 ClickableSpan 的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TextView 有很多 ClickableSpan.

I have a TextView with many ClickableSpan.

点击一个ClickableSpan,我必须得到它屏幕上的坐标(在他的位置显示一个自定义的View).

On click on a ClickableSpan, I have to get the coordinate on screen of it (to show a custom View at his position).

问题是我不知道该怎么做.ClickableSpanonClick() 方法在参数中为我提供了一个 View,即包含 TextViewTextView>ClickableSpan.

The problem is that I have no idea of how I can do this. The onClick() method of the ClickableSpan gives me in parameter a View, the TextView which contains the ClickableSpan.

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

I have used the following to get characters position in the TextView, but I don't know how I can convert it to get x/y position on screen of the text.

@Override
public void onClick(View v){

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

}

提前感谢您的帮助!

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

EDIT : I want to get the entire coordinate of the ClickableSpan and its size, the aim is to show my custom view at the bottom center of the text. The onTouch method will give me the finger position, not the entire text coordinates. So, with this method, I will not have the middle of the text.

推荐答案

我找到了解决方案 :-)在这里,也许这会帮助像我一样有同样问题的人!

I have found a solution :-) Here it is, may be this will help others with the same problem like me !

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

这篇关于如何在 TextView 中获取 ClickableSpan 的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆