2017-04-13 15 views
0

我有一个编辑文本,它的行为与面书中的提要类似。因此,无论何时我发布像“www.google.com”这样的内容,它都会显示为超链接,但同时我只要发布类似“abcd.abcd”的内容,它也会显示为超链接。我只想在添加“http”或“www”时将链接显示为链接,如何实现此目的。如果在此先感谢显示为超链接的点的文本

<EditText 
       android:id="@+id/etFeedsText" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:minHeight="50dp" 
       android:maxLength="2000" 
       android:layout_marginBottom="25.8dp" 
       android:layout_marginLeft="14dp" 
       android:layout_marginTop="8.3dp" 
       android:hint="@string/enter_your_post" 
       android:background="@color/white" 
       android:inputType="textMultiLine|textNoSuggestions" 
       android:textColor="@color/feeds_text" 
       android:textSize="13.3sp" />  

回答

2

您可以比较文字&可以找到一个有效的URL或不通过这种方法,

如果文字不是一个URL然后从底部下划线像,

if(!isValidUrl(yourUrl)){ 
    stripUnderlines(TextView textView) 
} 


private void stripUnderlines(TextView textView) { 
     Spannable s = new SpannableString(textView.getText()); 
     URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class); 
     for (URLSpan span: spans) { 
      int start = s.getSpanStart(span); 
      int end = s.getSpanEnd(span); 
      s.removeSpan(span); 
      span = new URLSpanNoUnderline(span.getURL()); 
      s.setSpan(span, start, end, 0); 
     } 
     textView.setText(s); 
    } 

它需要URLSpan的定制版本不启用TextPaint的“下划线”属性:

private class URLSpanNoUnderline extends URLSpan { 
     public URLSpanNoUnderline(String url) { 
      super(url); 
     } 
     @Override public void updateDrawState(TextPaint ds) { 
      super.updateDrawState(ds); 
      ds.setUnderlineText(false); 
     } 
    } 

希望得到这个帮助!

+0

嘿谢谢威尔逊。我会尽力让你知道。再次感谢 –

1

只需添加到您的EDITTEXT

Linkify.addLinks(etFeedsText, Linkify.WEB_URLS); 

这将只考虑有效的URL。 如果你只是显示数据,你也可以使用textview。 Edittext是没有必要的。