2014-03-28 42 views
3

我有這樣一個TextView:如何在textView和自定義URLSpan中一起使用autoLink?

<TextView 
    android:id="@+id/note_viewer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:autoLink="email|web|phone" 
    android:textSize="15sp" /> 

而一個Spannable字符串是這樣的:

String input = note.getText(); 
SpannableStringBuilder builder = new SpannableStringBuilder(input); 
Pattern pattern = Pattern.compile(XTAG_PATTERN); 
Matcher matcher = pattern.matcher(input); 
while (matcher.find()) { 
    int start = matcher.start(); 
    int end = matcher.end(); 
    String text = input.subSequence(start, end).toString(); 
    ClickableURLSpan url = new ClickableURLSpan(text, getActivity() 
    .getApplicationContext(), this); 
    builder.setSpan(url, start, end, 0); 
} 
noteView.setText(builder); 
noteView.setMovementMethod(LinkMovementMethod.getInstance()); 

和一個自定義`URLSpan類是這樣的:

public class ClickableURLSpan extends URLSpan { 
    Context context; 
    NoteViewFragment noteViewFragment; 
    public ClickableURLSpan(String url,Context c, NoteViewFragment noteViewFragment) { 
    super(url); 
    context=c; 
    this.noteViewFragment=noteViewFragment; 
} 
@Override 
public void onClick(View widget) { 
    String clickedText = getURL(); 
    Intent i=new Intent(context,SearchActivity.class); 
    i.putExtra("tag", clickedText); 
    noteViewFragment.startActivity(i); 
}} 

但是,當我m使用自動鏈接我的自定義可點擊跨度不起作用。我如何將自動鏈接和我自己的可點擊跨度一起使用?

+0

字符串輸入= ...,字符串沒有被跨區卷,跨度被保留使用跨區IFACE – pskink

+0

如果我在文本視圖 – sajad

+0

我現在看到刪除自動連接它工作正常時,你設置你的自定義跨度? – pskink

回答

0

變化spanable字符串:

  String input = note.getText(); 
      Spannable spannable = new SpannableString(Html.fromHtml(input)); 
      Pattern pattern = Pattern.compile(XTAG_PATTERN); 

      Linkify.addLinks(spannable, pattern, ""); 
      URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class); 
      for (URLSpan urlSpan : spans) { 
       ClickableURLSpan linkSpan = new ClickableURLSpan(text, getActivity().getApplicationContext(), this); 
       int spanStart = spannable.getSpanStart(urlSpan); 
       int spanEnd = spannable.getSpanEnd(urlSpan); 
       spannable.setSpan(linkSpan, spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
       spannable.removeSpan(urlSpan); 
      } 
      textview.setMovementMethod(EnhancedLinkMovementMethod.getInstance()); 
      noteView.setText(spannable, TextView.BufferType.SPANNABLE); 
相關問題