一个TextView的文本颜色,使用两个Linkify和可链接的weblinks

 明天会更好--好过_652 发布于 2022-12-09 16:03

我希望twitter提及红色,而主题标签是另一种颜色,如果任何推文包含任何网页链接..链接应该是可点击的并通过意图传递给另一个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"));

在此输入图像描述

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

    NonUnderlinedClickableSpan

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

    2022-12-11 02:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有