2017-09-21 57 views
1

我在android textview中有一個鏈接。我無法捕獲鏈接點擊事件。在android textview中捕獲http鏈接點擊事件

String text = "http:://www.google.com is a google link"; 
textview.setText(text); 

「http :: //www.google.com」這段字符串在textview中可點擊。我想捕獲特定的點擊事件。

我嘗試了以下。

public static void setTextView(TextView text, CharSequence sequence) { 
    UoloLogger.i(TAG, "Setting string :: "+sequence); 
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence); 
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class); 
    for(URLSpan span : urls) { 
     makeLinkClickable(strBuilder, span); 
    } 
    text.setText(strBuilder); 
    text.setMovementMethod(LinkMovementMethod.getInstance()); 
} 

public static void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) { 
    int start = strBuilder.getSpanStart(span); 
    int end = strBuilder.getSpanEnd(span); 
    int flags = strBuilder.getSpanFlags(span); 
    ClickableSpan clickable = new ClickableSpan() { 
     public void onClick(View view) { 
      UoloLogger.i(TAG, span.getURL()); 
     } 
    }; 
    strBuilder.setSpan(clickable, start, end, flags); 
    strBuilder.removeSpan(span); 
} 

我開始使用setTextView()方法將文本設置到我的textview中。即使我有鏈接,我得到的URLSpan數組也是空的。

String text = "http:://www.google.com is a google link"; 
setTextView(textView, text); 

對不起,這個英文不好。我想,我已經解釋了我的問題。有人能幫我嗎。

+0

遵照Arunkumar https://stackoverflow.com/questions/7722806/get-the-value-of-link-text-when-clicked-in-a-textview-in-android這裏給出的步驟 –

+1

Follow this: https://stackoverflow.com/questions/7722806/get-the-value-of-link-text-when-clicked-in-a-textview-in-android –

回答

2
public static void setLinkclickEvent(TextView tv, HandleLinkClickInsideTextView clickInterface) { 
    String text = tv.getText().toString(); 
    String str = "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])"; 
    Pattern pattern = Pattern.compile(str); 
    Matcher matcher = pattern.matcher(tv.getText()); 
    while (matcher.find()) { 
     int x = matcher.start(); 
     int y = matcher.end(); 
     final android.text.SpannableString f = new android.text.SpannableString(
       tv.getText()); 
     InternalURLSpan span = new InternalURLSpan(); 
     span.setText(text.substring(x, y)); 
     span.setClickInterface(clickInterface); 
     f.setSpan(span, x, y, 
       android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
     tv.setText(f); 
    } 
    tv.setLinksClickable(true); 
    tv.setMovementMethod(LinkMovementMethod.getInstance()); 
    tv.setFocusable(false); 
} 

public static class InternalURLSpan extends android.text.style.ClickableSpan { 

    private String text; 
    private HandleLinkClickInsideTextView clickInterface; 

    @Override 
    public void onClick(View widget) { 
     getClickInterface().onLinkClicked(getText()); 
    } 

    public void setText(String textString) { 
     this.text = textString; 
    } 

    public String getText() { 
     return this.text; 
    } 

    public void setClickInterface(HandleLinkClickInsideTextView clickInterface) { 
     this.clickInterface = clickInterface; 
    } 

    public HandleLinkClickInsideTextView getClickInterface() { 
     return this.clickInterface; 
    } 

} 

public interface HandleLinkClickInsideTextView { 
    public void onLinkClicked(String url); 
} 

在此之後我只是用的方法來發送點擊事件。

textview.setText("http://google.com is google website and http://youtube.com is youtube site"); 
setLinkclickEvent(textview, new HandleLinkClickInsideTextView() { 
    public void onLinkClicked(String url) { 
      // Here I added my code 
    } 
}); 
1

如果你想打開的TextView點擊後鏈接,有兩種選擇:

  1. 使用Java代碼:

    Spanned text = Html.fromHtml("<u>GOOGLE.COM</u>"); 
    textView.setText(text); 
    Uri uri = Uri.parse("http://shopwhere.com.au/"); 
    Intent webIntent = new Intent(Intent.ACTION_VIEW,uri); 
    // Create and start the chooser 
    Intent chooser = Intent.createChooser(webIntent, "Open with"); 
    startActivityForResult(chooser,0); 
    
  2. 使用XML:

    在textview標籤內使用android:autoLink="web"。您還可以更改鏈接顏色android:textColorHighlight="@android:color/transparent"android:textColorLink="@color/white"

+1

我可以打開鏈接。我想捕捉哪個鏈接點擊了,我想執行一些代碼。 –

0

使用SpannableStringBuilder可以達到同樣的效果。

簡單的初始化要添加2個或更多的聽衆,然後傳遞到我創建了以下方法的TextView:

示例代碼:

private void customTextView(TextView view) { 
     SpannableStringBuilder spanTxt = new SpannableStringBuilder(
       "I agree to the "); 
     spanTxt.append("Term of services"); 
     spanTxt.setSpan(new ClickableSpan() { 
      @Override 
      public void onClick(View widget) { 
       Toast.makeText(getApplicationContext(), "Terms of services Clicked", 
         Toast.LENGTH_SHORT).show(); 
      } 
     }, spanTxt.length() - "Term of services".length(), spanTxt.length(), 0); 
     spanTxt.append(" and"); 
     spanTxt.setSpan(new ForegroundColorSpan(Color.BLACK), 32, spanTxt.length(), 0); 
     spanTxt.append(" Privacy Policy"); 
     spanTxt.setSpan(new ClickableSpan() { 
      @Override 
      public void onClick(View widget) { 
       Toast.makeText(getApplicationContext(), "Privacy Policy Clicked", 
         Toast.LENGTH_SHORT).show(); 
      } 
     }, spanTxt.length() - " Privacy Policy".length(), spanTxt.length(), 0); 
     view.setMovementMethod(LinkMovementMethod.getInstance()); 
     view.setText(spanTxt, BufferType.SPANNABLE); 
    } 

而在你的XML ,請使用android:textColorLink添加您選擇的自定義鏈接顏色。就像這樣:

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" 
    android:textColorLink="#C36241" /> 
相關問題