Android中点击TextView文本链接跳转到指定页面

个人小知识
献上 效果图:
在这里插入图片描述

平常的话 可能 会几个控件去拼接得到效果图
今年 发现了一个 另外一种写法:

 <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/mine_tools_help_feed_tv_bot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        android:autoLink="all"
        android:gravity="center"
        android:lineSpacingExtra="5dp"
        android:textColor="#222222"
        android:textSize="14sp"
        tools:ignore="HardcodedText" />

AppCompatTextView 也可以换成 textview
接下来就是主要内容了

/**
     * 获取可点击的SpannableString
     * @return
     */
    private SpannableString getClickableSpan() {
        SpannableString spannableString = new SpannableString("使用该软件," +"\n"+
                "即表示您同意该软件的《使用条款》和《隐私政策》");
        //设置下划线文字
    //    spannableString.setSpan(new UnderlineSpan(), 17, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的单击事件
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                intent.putExtra("title", "使用条款");
                intent.putExtra("url", "https://blog.csdn.net/chengsiguo/article/details/88422311");
                startActivity(intent);
            }
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);//当传入true时超链接下会有一条下划线
            }
        }, 17, 23,  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的前景色
        spannableString.setSpan(new ForegroundColorSpan(App.getColor2(R.color.green_a2)), 17, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置下划线文字
      //  spannableString.setSpan(new UnderlineSpan(), 24, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的单击事件
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                intent.putExtra("title", "隐私政策");
                intent.putExtra("url", "https://blog.csdn.net/chengsiguo/article/details/88422311");
                startActivity(intent);
            }
            @Override
            public void updateDrawState(TextPaint ds) {

                ds.setUnderlineText(false);//当传入true时超链接下会有一条下划线
                ds.setColor(App.getColor2(R.color.viewfinder_mask_two));
            }
        }, 24, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的前景色
        spannableString.setSpan(new ForegroundColorSpan(App.getColor2(R.color.green_a2)), 24, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

使用方法是:

控件id 

      //借助SpannableString类实现超链接文字
        mineToolsHelpFeedTvBot.setText(getClickableSpan());
        //设置超链接可点击
        mineToolsHelpFeedTvBot.setMovementMethod(LinkMovementMethod.getInstance());
        //设置点击时的背景色为透明
        mineToolsHelpFeedTvBot.setHighlightColor(ContextCompat.getColor(this,R.color.viewfinder_mask_two));

还有另一种写法: 使用方法是一样的

 private CharSequence checkAutoLink() {
     //设置的文字
        String content = getResources().getString(R.string.mine_aboutus_tv_yinsi);
        ssb = new SpannableStringBuilder(content);
        Pattern pattern = Pattern.compile("隐私政策"); //根据正则匹配出带有超链接的文字
        Matcher matcher = pattern.matcher(ssb);
        while (matcher.find()) {
            setClickableSpan(ssb, matcher);
        }
        return ssb;
    }

    private void setClickableSpan(final SpannableStringBuilder clickableHtmlBuilder, final Matcher matcher) {
        int start = matcher.start();
        int end = matcher.end();
        final String url = "https://blog.csdn.net/chengsiguo/article/details/88422311";
        ClickableSpan clickableSpan = new ClickableSpan() {
            public void onClick(View view) {
                //实现跳转逻辑
               /* */

                gotoUrl("https://blog.csdn.net/chengsiguo/article/details/88422311");
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);//当传入true时超链接下会有一条下划线
            }
        };
        //设置超链接文本的颜色
        ssb.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorAccent)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        clickableHtmlBuilder.setSpan(clickableSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    }

    private void gotoUrl(String url) {
        ToastUtils.show("asd");
        intent.putExtra("title", "资讯相关");
        intent.putExtra("url", "https://blog.csdn.net/chengsiguo/article/details/88422311");
        startActivity(intent);
    }

最后附带 :[SpannableString使用详解]

文章参考链接是:https://blog.csdn.net/zhangjinhuang/article/details/52416608
https://blog.csdn.net/zhaohaiyitian88/article/details/60353515
https://blog.csdn.net/lyankj/article/details/51882335

嗯嗯 重点来了 如果 有帮助的话 帮忙点个赞
装载的话 可以标注一下地址就可

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值