【问题标题】:text colors for one TextView using two Linkify's and weblinks clickable一个 TextView 的文本颜色,使用两个 Linkify 和可点击的网络链接
【发布时间】:2014-11-26 09:54:34
【问题描述】:

如果任何推文包含任何网络链接,我希望 twitter 提及为红色,而 hashtags 是另一种颜色。链接应该是可点击的,并通过意图传递给另一个 Activity(WebView)。

我怎样才能做到这一点??

   TransformFilter filter = new TransformFilter() {
                public final String transformUrl(final Matcher match, String url) {
                    return match.group();
                }
            };

            Pattern mentionPattern = Pattern.compile("@([A-Za-z0-9_-]+)");
            String mentionScheme = "http://www.twitter.com/";
            Linkify.addLinks(tvMessage, mentionPattern, mentionScheme, null, filter);


            Pattern hashtagPattern = Pattern.compile("#([A-Za-z0-9_-]+)");
            String hashtagScheme = "http://www.twitter.com/search/";
            Linkify.addLinks(tvMessage, hashtagPattern, hashtagScheme, null, filter);

            Pattern urlPattern = Patterns.WEB_URL;
            Linkify.addLinks(tvMessage, urlPattern, null, null, filter);

 // tvMessage.setLinkTextColor(Color.parseColor("#3467BB"));

【问题讨论】:

  • 也许你正在寻找类似这样的 Html.fromHtml()

标签: android twitter linkify


【解决方案1】:

Finally i have achieved hashtags weblinks clickable and made them very attractive by keeping different colors 使用 android sdk SpannableString 并且它是 ClickableSpan 。

private void Linkfiy(String a, TextView textView) {

        Pattern urlPattern = Patterns.WEB_URL;
        Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)");
        Pattern hashtagPattern = Pattern.compile("#(\\w+|\\W+)");

        Matcher o = hashtagPattern.matcher(a);
        Matcher mention = mentionPattern.matcher(a);
        Matcher weblink = urlPattern.matcher(a);


        SpannableString spannableString = new SpannableString(a);
        //#hashtags

        while (o.find()) {

            spannableString.setSpan(new NonUnderlinedClickableSpan(o.group(),
                    0), o.start(), o.end(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        }

        // --- @mention
        while (mention.find()) {
            spannableString.setSpan(
                    new NonUnderlinedClickableSpan(mention.group(), 1), mention.start(), mention.end(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        }
        //@weblink
        while (weblink.find()) {
            spannableString.setSpan(
                    new NonUnderlinedClickableSpan(weblink.group(), 2), weblink.start(), weblink.end(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        }

        textView.setText(spannableString);
        textView.setMovementMethod(LinkMovementMethod.getInstance());


    }

NonUnunderlinedClickableSpan

 public class NonUnderlinedClickableSpan extends ClickableSpan {

            int type;// 0-hashtag , 1- mention, 2- url link
            String text;// Keyword or url
            String time;

            public NonUnderlinedClickableSpan(String text, int type) {
                this.text = text;
                this.type = type;
                this.time = time;
            }

            @Override
            public void updateDrawState(TextPaint ds) {
        //adding colors 
                if (type == 1)
                    ds.setColor(InstagramIndetail.this.getResources().getColor(
                            R.color.link_color_mention));
                else if (type == 2)
                    ds.setColor(InstagramIndetail.this.getResources().getColor(
                            R.color.link_color_url));
                else
                    ds.setColor(InstagramIndetail.this.getResources().getColor(
                            R.color.link_color_hashtag));
                ds.setUnderlineText(false);
                // ds.setTypeface(Typeface.DEFAULT_BOLD);
            }

            @Override
            public void onClick(View v) {

                Debug.e("click done", "ok " + text);
                if (type == 0) {
                    //pass hashtags to activity using intents 
                } else if (type == 1) {
                     //do for mentions 
                } else {
 // passing weblinks urls to webview activity
                    startWebViewActivity(text);
                }
            }
        }

【讨论】:

  • 这是一个很好的解决方案。非常感谢。
  • 小修正,因为如果用户一起输入多个主题标签,此解决方案将不起作用,例如##xyz, ####abc 所以我建议这个stackoverflow.com/a/63420705/3598052 hahstag 解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
相关资源
最近更新 更多