2013-11-27 65 views
5

我有一個TexView需要放置在格言2行,並有一個可鏈接的文本。 如果我將LinkMovementMethod設置爲文本視圖,我會得到一個可滾動的TextView,並忽略橢圓大小。使橢圓化一個TextView有LinkMovementMethod

XML代碼:

<TextView 
      android:id="@+id/text_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="4dip" 
      android:clickable="true" 
      android:maxLines="2" 
      android:ellipsize="end" 

      android:scrollHorizontally="false" 
      android:scrollbars="horizontal" 
      android:isScrollContainer="false"/> 

這是我的活動:

public class MyActivity extends Activity { 

    private TextView tv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     tv = (TextView) findViewById(R.id.text_view); 

     SpannableStringBuilder captionSpan = new SpannableStringBuilder(); 
     captionSpan.append("a very long text here a very long text here a very long text here a very long text here a very long text here a very long text here a very long text here a very long text here"); 
     captionSpan.setSpan(new CustomClickableSpan(), 1, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

     tv.setText(captionSpan); 
     tv.setMovementMethod(LinkMovementMethod.getInstance()); 
    } 

} 


public class CustomClickableSpan extends android.text.style.ClickableSpan { 

    @Override 
    public void updateDrawState(TextPaint tp) { 
     tp.setColor(tp.linkColor); 
     tp.setUnderlineText(false); 
    } 

    @Override 
    public void onClick(View widget) { 
    } 

} 

如果我不設置移動方法一切工作正常。

回答

4

我找到解決方案!
而是setMovementMethod()使用OnTouchListener

String text = textView.getText().toString(); 
SpannableString spanText = new SpannableString(text); 

//here set your spans to spanText 

textView.setOnTouchListener(new TouchTextView(spanText)); 
textView.setText(spanText); 

我得到這個的onTouchEvent()LinkMovementMethod類。

static class TouchTextView implements View.OnTouchListener { 
    Spannable spannable; 

    public TouchTextView (Spannable spannable){ 
     this.spannable = spannable; 
    } 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     int action = event.getAction(); 
     if(!(v instanceof TextView)){ 
      return false; 
     } 
     TextView textView = (TextView) v; 
     if (action == MotionEvent.ACTION_UP || 
       action == MotionEvent.ACTION_DOWN) { 
      int x = (int) event.getX(); 
      int y = (int) event.getY(); 

      x -= textView.getTotalPaddingLeft(); 
      y -= textView.getTotalPaddingTop(); 

      x += textView.getScrollX(); 
      y += textView.getScrollY(); 

      Layout layout = textView.getLayout(); 
      int line = layout.getLineForVertical(y); 
      int off = layout.getOffsetForHorizontal(line, x); 

      ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class); 

      if (link.length != 0) { 
       if (action == MotionEvent.ACTION_UP) { 
        link[0].onClick(textView); 
       } else if (action == MotionEvent.ACTION_DOWN) { 
        Selection.setSelection(spannable, 
          spannable.getSpanStart(link[0]), 
          spannable.getSpanEnd(link[0])); 
       } 

       return true; 
      } else { 
       Selection.removeSelection(spannable); 
      } 
     } 

     return false; 
    } 
} 

而且肯定的TextView應該有這樣的屬性:

android:maxLines="2" 
android:ellipsize="end" 
+0

我嚴重不知道該怎麼感謝你纔好!這救了我! – Cbr

+0

我很高興幫助你) – Airfreshener

+0

終於明白了..非常感謝你... –