2012-08-10 25 views
12

Birçok ClickableSpan ile TextView var.Bir TextView içinde bir ClickableSpan'ın koordinatını nasıl edinirsiniz?

Bir ClickableSpan numaralı tıklamayla, ekranda koordinatlarını almalıyım (özel bir View numaralı telefon numarasını kullanarak). Sorun şu ki, bunu nasıl yapabileceğimi bilmiyorum. ClickableSpan'un onClick() yöntemi, , ClickableSpan'u içeren TextView parametresini verir.

TextView'da karakterlerin konumunu almak için aşağıdakileri kullandım, ancak metnin ekranında x/y konumuna geçmek için bunu nasıl değiştirebileceğimi bilmiyorum.

@Override 
public void onClick(View v){ 

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

} 

Yardımlarınız için şimdiden teşekkür ederiz!

DÜZENLEME: ClickableSpan ve boyutunun tüm koordinatını almak istiyorum, amacım metnin alt merkezinde özel görünümümü göstermektir. OnTouch yöntemi, tüm metin koordinatlarını değil, parmak pozisyonunu verir. Yani, bu yöntemle, metnin ortasına sahip olmayacağım.

+0

onTouch() işlevini ve event.getX() öğesini kullanın, event.getY()? –

+1

Cevabınız için teşekkürler! Ne yazık ki, bu yöntemi çok ... çirkin buldum! Umarım daha iyi bir çözüm vardır. Dahası, başka bir hassaslık vermeyi de unutmuşum: ClickableSpan'ın tüm koordinatını ve büyüklüğünü elde etmek istiyorum. Amaç, özel görünümümü metnin alt merkezinde göstermektir. OnTouch yöntemi, tüm metin koordinatlarını değil, parmak pozisyonunu verir. Yani, bu yöntemle, metnin ortasına sahip olmayacağım. Başka bir fikrin var mı? –

cevap

25

Bir çözüm buldum :-) İşte bu, benden aynı sorun yaşayan diğer kişilere yardımcı olabilir!

// 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 
); 
İlgili konular