Android 使用两个Linkify';s和Web链接可单击

Android 使用两个Linkify';s和Web链接可单击,android,twitter,linkify,Android,Twitter,Linkify,我希望twitter上提到的内容是红色的,如果任何tweet包含任何web链接,hashtags是另一种颜色。。链接应该是可点击的,并通过意图传递到另一个活动(WebView) 我怎样才能做到这一点 TransformFilter filter = new TransformFilter() { public final String transformUrl(final Matcher match, String url) {

我希望twitter上提到的内容是红色的,如果任何tweet包含任何web链接,hashtags是另一种颜色。。链接应该是可点击的,并通过意图传递到另一个活动(
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"));

最后,我实现了hashtags网页链接的可点击性,并通过使用android sdk span保持不同的颜色使它们非常吸引人span

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());


    }
无下划线可点击span

 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);
                }
            }
        }

也许您正在寻找类似于Html的内容。fromHtml()这是一个很好的解决方案。非常感谢你。